首页 > 代码库 > MPMoviePlayerViewController和MPMoviePlayerController的使用
MPMoviePlayerViewController和MPMoviePlayerController的使用
ios播放视频文件一般使用 MPMoviePlayerViewController 和 MPMoviePlayerController。前者是一个view,后者是个Controller。区别就是MPMoviePlayerViewController里面包含了一个MPMoviePlayerController
先说MPMoviePlayerController
首先要包含 #import <MediaPlayer/MediaPlayer.h>头文件和MediaPlayer.framework。
- (void)createMPPlayerController:(NSString *)sFileNamePath {
NSURL *movieURL = [NSURL fileURLWithPath:sFileNamePath];
MPMoviePlayerController *movewController =[[MPMoviePlayerController alloc] initWithContentURL:movieURL];
[movewController prepareToPlay];
[self.view addSubview:movewController.view];//设置写在添加之后 // 这里是addSubView
movewController.shouldAutoplay=YES;
[movewController setControlStyle:MPMovieControlStyleDefault];
[movewController setFullscreen:YES];
[movewController.view setFrame:self.view.bounds];
这里注册相关操作的通知
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(movieFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moveViewController.movewController]; //播放完后的通知
[movewController release];
}
-(void)movieFinishedCallback:(NSNotification*)notify {
MPMoviePlayerController* theMovie = [notify object];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:theMovie];
[theMovie.view removeFromSuperview];
[theMovie release];
}
//////////////////////////////// end
2.介绍下MPMoviePlayerViewController。
注意:MPMoviePlayerViewController 必须 presentMoviePlayerViewControllerAnimated方式添加,否则Done按钮是不会响应通知MPMoviePlayerPlaybackDidFinishNotification事件的;
- (void)createMPPlayerController:(NSString *)sFileNamePath {
MPMoviePlayerViewController *moviePlayer =[[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL fileURLWithPath:sFileNamePath]];
[moviePlayer.moviePlayer prepareToPlay];
[self presentMoviePlayerViewControllerAnimated:moviePlayer]; // 这里是presentMoviePlayerViewControllerAnimated
[moviePlayer.moviePlayer setControlStyle:MPMovieControlStyleFullscreen];
[moviePlayer.view setBackgroundColor:[UIColor clearColor]];
[moviePlayer.view setFrame:self.view.bounds];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(movieFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer.moviePlayer];
[moviePlayer release];
}
-(void)movieStateChangeCallback:(NSNotification*)notify {
//点击播放器中的播放/ 暂停按钮响应的通知
}
-(void)movieFinishedCallback:(NSNotification*)notify{
// 视频播放完或者在presentMoviePlayerViewControllerAnimated下的Done按钮被点击响应的通知。
MPMoviePlayerController* theMovie = [notify object];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:theMovie];
[self dismissMoviePlayerViewControllerAnimated];
}
以下是资料
moviePlayer.moviewControlMode = MPMovieControlModeDefault;
MPMovieControlModeDefault 显示播放/暂停、音量和时间控制
MPMovieControlModeVolumeOnly 只显示音量控制
MPMovieControlModeHidden 没有控制器
moviePlayer.scallingMode = MPMovieScallingModeAspectFit;
你可以使用下列宽高比值:
MPMovieScallingModeNone 不做任何缩放
MPMovieScallingModeAspectFit 适应屏幕大小,保持宽高比
MPMovieScallingModeAspectFill 适应屏幕大小,保持宽高比,可裁剪
MPMovieScallingModeFill 充满屏幕,不保持宽高比
你会观察到以下通知:
MPMoviePlayerContentPreloadDidFinishNotification
当电影播放器结束对内容的预加载后发出。因为内容可以在仅加载了一部分的情况下播放,所以这个通知可能在已经播放后才发出。
MPMoviePlayerScallingModeDidChangedNotification
当用户改变了电影的缩放模式后发出。用户可以点触缩放图标,在全屏播放和窗口播放之间切换。
MPMoviePlayerPlaybackDidFinishNotification
当电影播放完毕或者用户按下了Done按钮后发出。