MPMoviePlayerController与AVPlayer

IOS9之前,ios播放视频文件一般使用 MPMoviePlayerViewController 和 MPMoviePlayerController。

前者继承自UIViewController,后者继承NSObject。MPMoviePlayerViewController里面包含了一个MPMoviePlayerController。

要想使用上述两者,首先要包含  #import头文件和MediaPlayer.framework。

MPMoviePlayerViewController中只有三个方法和一个属性:

//初始化

- (instancetype)initWithContentURL:(NSURL *)contentURL

//把这个VC Present出来

- (void)presentMoviePlayerViewControllerAnimated:(MPMoviePlayerViewController *)moviePlayerViewController

//把这个VC dismiss掉

- (void)dismissMoviePlayerViewControllerAnimated

//包含的MPMoviePlayerController

@property (nonatomic, readonly) MPMoviePlayerController *moviePlayer;

从根本意义上来说,MPMoviePlayerViewController的方法实现的就是MPMoviePlayerController中的对应初始化,加入视图的方法,前者只是对后者进行了一个简单的封装。

如果要对播放视频的属性进行操作,可以通过设置MPMoviePlayerViewController.moviePlayer来实现。MPMoviePlayerController中可以的修改的参数有很多,详细可以去看官网文档,或者直接从Xcode中直接进mediaPlayer库文件中查看。

以下是关于播放视频的监听事件,注册之后,当对应的状态改变时就可以收到对应的通知

// -----------------------------------------------------------------------------

// Movie Player Notifications

// 当视频缩放比例改变时

 NSString * const MPMoviePlayerScalingModeDidChangeNotification 

// 当视频播放结束时

 NSString * const MPMoviePlayerPlaybackDidFinishNotification 

//当用户退出视频时

 NSString * const MPMoviePlayerPlaybackDidFinishReasonUserInfoKey 

// 当回调状态改变时

 NSString * const MPMoviePlayerPlaybackStateDidChangeNotification 

// 当网络加载状态改变时

 NSString * const MPMoviePlayerLoadStateDidChangeNotification 

//当当前播放视频改变时

 NSString * const MPMoviePlayerNowPlayingMovieDidChangeNotification 

// 当进入全屏或者退出全屏

 NSString * const MPMoviePlayerWillEnterFullscreenNotification 

 NSString * const MPMoviePlayerDidEnterFullscreenNotification 

 NSString * const MPMoviePlayerWillExitFullscreenNotification 

 NSString * const MPMoviePlayerDidExitFullscreenNotification 

 NSString * const MPMoviePlayerFullscreenAnimationDurationUserInfoKey 

 NSString * const MPMoviePlayerFullscreenAnimationCurveUserInfoKey 

// 在appleTv或者音响上播放状态改变时

 NSString * const MPMoviePlayerIsAirPlayVideoActiveDidChangeNotification 

// 当准备状态改变时

 NSString * const MPMoviePlayerReadyForDisplayDidChangeNotification 

播放本地路径下的视频的实例代码如下

- (void)Play:(NSString*)resName TypeName:(NSString*)type

{

NSString *path = [[NSBundle mainBundle] pathForResource:resName ofType:type];

if ( nil == path )  {

return;

}

NSURL *url = [NSURL fileURLWithPath:path];

MPMoviePlayerViewController *_moviePlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:url];

_moviePlayer.moviePlayer.controlStyle = MPMovieControlStyleNone;

[[NSNotificationCenter defaultCenter] addObserver:self

selector:@selector(movieFinishedCallback:)

name:MPMoviePlayerPlaybackDidFinishNotification

object:[_moviePlayer moviePlayer] ];

[_moviePlayer.moviePlayer play];

[ [self GetRootViewController] presentMoviePlayerViewControllerAnimated:_moviePlayer];

_moviePlayer = nil;

}

- (void) movieFinishedCallback:(NSNotification*) aNotification

{

MPMoviePlayerController *player = [aNotification object];

[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:player];

[player stop];

[ [self GetRootViewController] dismissMoviePlayerViewControllerAnimated];

}

在IOS9之后,上述的MPMoviePlayerController就被苹果弃用了(不过不影响正常使用),苹果推荐使用AVPlayerViewController,简而言之就是MPMoviePlayerController使用更简单,功能不如AVPlayer强大,而AVPlayer使用稍微麻烦点,不过功能更加强大。

实例代码

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"backspace" ofType:@"mov"];

NSURL *sourceMovieURL = [NSURL fileURLWithPath:filePath];

AVAsset *movieAsset = [AVURLAsset URLAssetWithURL:sourceMovieURL options:nil];

AVPlayerItem *playerItem = [AVPlayerItem playerItemWithAsset:movieAsset];

AVPlayer *player = [AVPlayer playerWithPlayerItem:playerItem];

AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:player];

playerLayer.frame = self.view.layer.bounds;

playerLayer.videoGravity = AVLayerVideoGravityResizeAspect;

[self.view.layer addSublayer:playerLayer];

[player play];

上方的代码实现的效果其实和MPMoviePlayerController实现的是一样的,AVPlayer更强大的地方是它有对应的方法去调节视频的音量以及视频的进度,可以通过简单的UIslider来实现一个简单的播放器,是不是很酷炫呢!(具体的调节方法请参照苹果API)

你可能感兴趣的:(MPMoviePlayerController与AVPlayer)