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方式加入,否则Donebutton是不会响应通知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 {
//点击播放器中的播放/ 暂停button响应的通知
}
-(void)movieFinishedCallback:(NSNotification*)notify{
// 视频播放完或者在presentMoviePlayerViewControllerAnimated下的Donebutton被点击响应的通知。
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
当电影播放完成或者用户按下了Donebutton后发出。