视频播放(MPMediaPlayerController,AVPlayer,AVPlayerViewcontroller) for iOS
2015-10-27 15:13
489 查看
和音频播放一样,ios也提供个很多的API。如mediaPlayer.framework下的MPMediaPlayerController、AVFounditon.framework下的AVPlayer和AVKit下的AVPlayerViewcontroller。下面先看一下下图
有上面的三幅图可以看出,MPMovieplayerController已经在ios9.0中被废弃了,用来替代的是AVPlayerViewcontroller。尽管如此,但还是说一下比较熟悉的MPMovieplayerController.
1、MPMovieplayerController
<1>初始化MPMovieplayerController
// 创建本地URL(也可创建基于网络的URL)
NSURL* movieUrl = [[NSBundle
mainBundle]URLForResource:@"movie"
withExtension:@"mp4"];
// 使用指定URL创建MPMoviePlayerController
// MPMoviePlayerController将会播放该URL对应的视频
MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController
alloc] initWithContentURL:movieUrl];
<2>设置属性
/ 设置该播放器的控制条风格。
moviePlayer.controlStyle =
MPMovieControlStyleEmbedded;
// 设置该播放器的缩放模式
moviePlayer.scalingMode =
MPMovieScalingModeAspectFit;
<3>设置view的frame并添加到window上
[moviePlayer.view
setFrame: CGRectMake(0 ,
0 ,
380 , 320)];
[self.movieView
addSubview:
moviePlayer.view];
<4>播放暂停和停止
[moviePlayer
play];
[moviePlayer
pause];
[moviePlayer
stop];
由于属性太多,这里只列出能完成简单的播放器的属性,其他属性可在头文件中查找。
2、AVPlayer
AVPlayer既可以播放音乐又可以播放视频;使用AVPlayer不能直接显示视频,必须要加入AVPlayerLayer中,并添加到其他能显示的layer中。
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];
3、AVPlayerViewcontroller
AVPlayerViewcontroller继承自UIViewController,一般适用于点击一个视频缩略图,modal出一个新的界面来进行播放的情况。
用法:
AVPlayerViewController *player = [[AVPlayerViewController
alloc]init];
player.player = [[AVPlayer
alloc]initWithURL:movieUrl];
[self
presentViewController:player
animated:YES
completion:nil];
有上面的三幅图可以看出,MPMovieplayerController已经在ios9.0中被废弃了,用来替代的是AVPlayerViewcontroller。尽管如此,但还是说一下比较熟悉的MPMovieplayerController.
1、MPMovieplayerController
<1>初始化MPMovieplayerController
// 创建本地URL(也可创建基于网络的URL)
NSURL* movieUrl = [[NSBundle
mainBundle]URLForResource:@"movie"
withExtension:@"mp4"];
// 使用指定URL创建MPMoviePlayerController
// MPMoviePlayerController将会播放该URL对应的视频
MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController
alloc] initWithContentURL:movieUrl];
<2>设置属性
/ 设置该播放器的控制条风格。
moviePlayer.controlStyle =
MPMovieControlStyleEmbedded;
// 设置该播放器的缩放模式
moviePlayer.scalingMode =
MPMovieScalingModeAspectFit;
<3>设置view的frame并添加到window上
[moviePlayer.view
setFrame: CGRectMake(0 ,
0 ,
380 , 320)];
[self.movieView
addSubview:
moviePlayer.view];
<4>播放暂停和停止
[moviePlayer
play];
[moviePlayer
pause];
[moviePlayer
stop];
由于属性太多,这里只列出能完成简单的播放器的属性,其他属性可在头文件中查找。
2、AVPlayer
AVPlayer既可以播放音乐又可以播放视频;使用AVPlayer不能直接显示视频,必须要加入AVPlayerLayer中,并添加到其他能显示的layer中。
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];
3、AVPlayerViewcontroller
AVPlayerViewcontroller继承自UIViewController,一般适用于点击一个视频缩略图,modal出一个新的界面来进行播放的情况。
用法:
AVPlayerViewController *player = [[AVPlayerViewController
alloc]init];
player.player = [[AVPlayer
alloc]initWithURL:movieUrl];
[self
presentViewController:player
animated:YES
completion:nil];
相关文章推荐
- RPM包制作
- opencv 3.0 DPM Cascade 检测 (附带TBB和openMP加速)
- Activiti学习笔记三-开发部署bpmn流程文件
- B端产品经理的发展路径思考(间歇性更新完毕)
- npm 安装module
- 阿里PM的可用性测试秘籍:有理有据的用户体验优化
- rpm的安装与卸载
- 音频播放(System Sound Services、MPMusicPlayerController、AVAudioplayer) for IOS
- 概率矩阵分解模型 PMF
- APM飞控修改数传模块方法
- RPM包制作方法
- 用户体验学习笔记(工程中发现的PM常犯错误)
- PM俱乐部之旅7-弱活着
- 绩效考核:要想落地应该这样做
- npm ERR!无法安装任何包的解决办法
- centeOS6.5 RPM方式安装MySQL5.6
- cnetos 6----install ---- rpmfusion
- 如果我来应聘产品经理岗位(一)
- 听腾讯产品经理从 12306 和 “写简历” 这些事聊聊用户体验
- 关 于 yum rpm 的 类 习 题