iphone播放视频
加进去了以后,再给文件里添加一个视频文件video.m4v,注意扩展名,我先开始添加的视频文件的扩展名为mp4,结果一直播放的时候就是不播,所以我把视频转换成m4v扩展名就行了!
接下来是在PlayViewContrller.h文件中添加属性:
MPMoviePlayerController *player;//控制视频播放
然后是PlayViewController.m文件中添加代码实现:
[cpp] - (void)viewDidLoad
{
//获取视频文件的名称
NSString *url=[[NSBundle mainBundle]pathForResource:@"video" ofType:@"m4v"];
//初始化player,文件是video.m4v
player = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:url]];
//然后,您使用的NSNotificationCenter类注册通知,这样当影片完成播放(即结束)的时候调用movieFinishedCallback:方法;
[[NSNotificationCenter defaultCenter]
addObserver:self selector:@selector(movieFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification object:player];
//设置视频视图的大小
player.view.frame = CGRectMake(10, 10, 300, 300);
//添加控件
[self.view addSubview:player.view];
//播放影片
[player play];
[super viewDidLoad];
}
//电影停止播放时,你应该注销的通知,删除的影片,然后释放播放对象:
-(void)movieFinishedCallback:(NSNotification*) aNotification {
MPMoviePlayerController *moviePlayer = [aNotification object];
//注销通知
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];
//删除影片
[moviePlayer.view removeFromSuperview];
//释放
[player release];
}
- (void)viewDidLoad
{
//获取视频文件的名称
NSString *url=[[NSBundle mainBundle]pathForResource:@"video" ofType:@"m4v"];
//初始化player,文件是video.m4v
player = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:url]];
//然后,您使用的NSNotificationCenter类注册通知,这样当影片完成播放(即结束)的时候调用movieFinishedCallback:方法;
[[NSNotificationCenter defaultCenter]
addObserver:self selector:@selector(movieFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification object:player];
//设置视频视图的大小
player.view.frame = CGRectMake(10, 10, 300, 300);
//添加控件
[self.view addSubview:player.view];
//播放影片
[player play];
[super viewDidLoad];
}
//电影停止播放时,你应该注销的通知,删除的影片,然后释放播放对象:
-(void)movieFinishedCallback:(NSNotification*) aNotification {
MPMoviePlayerController *moviePlayer = [aNotification object];
//注销通知
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];
//删除影片
[moviePlayer.view removeFromSuperview];
//释放
[player release];
}
实现效果:
:
点击右下角放大图标
双击屏幕变成全屏:
点击Done按钮恢复原状!!!
ok!实现!本博客是我自己的练习,有好多地方没有讲太清楚,还请谅解!
摘自 任海丽(3G/移动开发)
相关新闻>>
- 发表评论
-
- 最新评论 更多>>