MPMoviePlayerViewController和MPMoviePlayerController的使用
2015-06-23 20:44
615 查看
转自:http://blog.csdn.net/baron_blogs/article/details/30462881?utm_source=tuicool
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按钮后发出。
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按钮后发出。
相关文章推荐
- 测试驱动开发实践 - Test-Driven Development
- 使用npm安装一些包失败了的看过来(npm国内镜像介绍)
- Windows环境下npm install常见错误
- ALSA声卡驱动中的DAPM详解之七:dapm事件机制(dapm event)
- ALSA声卡驱动中的DAPM详解之六:精髓所在,牵一发而动全身
- ALSA声卡驱动中的DAPM详解之五:建立widget之间的连接关系
- ALSA声卡驱动中的DAPM详解之四:在驱动程序中初始化并注册widget和route
- ALSA声卡驱动中的DAPM详解之三:如何定义各种widget
- ALSA声卡驱动中的DAPM详解之二:widget-具备路径和电源管理信息的kcontrol
- ALSA声卡驱动中的DAPM详解之一:kcontrol
- 产品经理就是导演
- RPM管理
- 如何开始做一个产品经理
- 工程PMO工作
- rpm -qs查看包信息
- rpm -qs查看包信息
- 为什么调用glPushMatrix()和glPopMatrix()
- 为什么调用glPushMatrix()和glPopMatrix()
- 【一些事晚报】产品经理必备技能
- 图像的稀疏表示——ScSPM和LLC的总结