您的位置:首页 > 产品设计 > 产品经理

使用MPMoviePlayerController播放视频

2015-06-19 09:10 771 查看

使用MPMoviePlayerController播放视频

分类:
iPhone开发 2012-01-31 12:30
17209人阅读 评论(4)
收藏
举报

objecturl文档fileapi测试

目录(?)[+]

MPMoviePlayerController播放视频
改进应用

MPMoviePlayerController播放视频

基本的视频播放,应该都是用MPMoviePlayerController吧,SDK文档里给出的例子也是这样的。

一般的步骤:

1. 准备好视频路径,创建MPMoviePlayerController,即使是本地视频文件,也要生成NSURL路径。

[cpp]
view plaincopyprint?

NSString *file = [[NSBundle mainBundle] pathForResource:@"test_movie" ofType:@"mp4"];
NSURL *url = [NSURL fileURLWithPath:file];
if (_moviePlayer == nil) {
_moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
}else {
[_moviePlayer setContentURL:url];
}

NSString *file = [[NSBundle mainBundle] pathForResource:@"test_movie" ofType:@"mp4"];
NSURL *url = [NSURL fileURLWithPath:file];
if (_moviePlayer == nil) {
_moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
}else {
[_moviePlayer setContentURL:url];
}


2. MPMoviePlayerController各种设置,拉伸、控制模式、循环模式、自动播放等等,自己看着API文档挨个儿试吧,总有一款适合你

!最后一步就是别忘了播放呗。

[cpp]
view plaincopyprint?

_moviePlayer.controlStyle = MPMovieControlStyleNone;
_moviePlayer.shouldAutoplay = YES;
_moviePlayer.repeatMode = MPMovieRepeatModeOne;
[_moviePlayer setFullscreen:YES animated:YES];
_moviePlayer.scalingMode = MPMovieScalingModeAspectFit;
[_moviePlayer play];

_moviePlayer.controlStyle = MPMovieControlStyleNone;
_moviePlayer.shouldAutoplay = YES;
_moviePlayer.repeatMode = MPMovieRepeatModeOne;
[_moviePlayer setFullscreen:YES animated:YES];
_moviePlayer.scalingMode = MPMovieScalingModeAspectFit;
[_moviePlayer play];


3. 在适当的位置开始/停止监听播放状态事件。

[cpp]
view plaincopyprint?

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:_moviePlayer];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:_moviePlayer];


监听视频播放完成的事件,播放完成将执行moviePlayBackDidFinish方法,在该方法中执行自己想要的操作。

因为我之前设置了循环播放、而且控制模式也是不可控制,因此播放器上是不会有“完成”按钮的。也就不会正常的通过完成播放来进入这个方法。

类似的,还可以监听MPMoviePlayerPlaybackStateDidChangeNotification等很多关于播放期间状态改变的事件,可以根据自己的需要去选择。

别忘了不需要时,记得removeObserver。

至此,简单的播放本地视频文件的操作就完成了,我还没有尝试过播放网络视频,不过应该也是同样的道理。这只是最基本的操作,如果深入使用应该还会涉及缓存处理、下载处理、播放控制等很多方面的细节技术,如果以后有机会做视频、音频方面的应用,再好好研究一下吧。

改进应用

之前给别人做了一个展会播放视频的小程序,就是用了上面的代码,结果遇到点儿小问题。

因为是展会播放视频,所以不允许用户控制,就放在那儿重复重复再重复的播放,但是测试最后突然发现个问题,虽然播放器上没有“完成”按钮了,但是还是能对手势操作进行响应的,在播放器上“两指捏合”,播放器就退出了,而且调试发现退出之前也不会走到moviePlayBackDidFinish方法里,呃!!!应该是在自己的viewcontroller里没有对手势操作行处理。这可咋办呢?

后来在sdk里发现了系统提供的MPMoviePlayerViewController类!!!太好了!!

代码改成:

1. new一个MPMoviePlayerViewController对象,将其view添加到我们自己的viewcontroller里面;

2. 不需要自己创建MPMoviePlayerController,使用MPMoviePlayerViewController里面的MPMoviePlayerController执行实际的播放操作;

3. 监听的对象改成2中所述的MPMoviePlayerController就行了。

[cpp]
view plaincopyprint?

// create MPMoviePlayerViewController

MPMoviePlayerViewController *playerViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:[playerViewController moviePlayer]];
// add to view
[self.view addSubview:playerViewController.view];

// play movie
MPMoviePlayerController *player = [playerViewController moviePlayer];
player.controlStyle = MPMovieControlStyleNone;
player.shouldAutoplay = YES;
player.repeatMode = MPMovieRepeatModeOne;
[player setFullscreen:YES animated:YES];
player.scalingMode = MPMovieScalingModeAspectFit;
[player play];

// create MPMoviePlayerViewController
MPMoviePlayerViewController *playerViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:[playerViewController moviePlayer]];
// add to view
[self.view addSubview:playerViewController.view];

// play movie
MPMoviePlayerController *player = [playerViewController moviePlayer];
player.controlStyle = MPMovieControlStyleNone;
player.shouldAutoplay = YES;
player.repeatMode = MPMovieRepeatModeOne;
[player setFullscreen:YES animated:YES];
player.scalingMode = MPMovieScalingModeAspectFit;
[player play];


这样就ok了。

当然,自己在viewController里面对手势操作进行捕捉和处理肯定也是没问题的,不过我对这块儿还不是太熟悉,呵呵,当时就走了个捷径。

手势操作是ios的特色,也是改进产品用户体验的大趋势,以后我也得好好学学这一块的技术!!!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: