IOS9.0的改變
棄用MPMoviePlayerViewController(導入的是MediaPlayer框架)
使用AVPlayerViewController(導入的是AVKit框架)
AVPlayer 是專門用來播放音視頻的類
AVPlayer的一些常用屬性
play
pause
跳轉(zhuǎn)進度 seekToTime:
currentItem 當前播放的視頻元素
volume 調(diào)節(jié)音量
externalPlaybackVideoGravity 視頻顯示的播放樣式
AVLayerVideoGravityResizeAspect 普通的
AVLayerVideoGravityResizeAspectFill充滿的
currentTime獲得當前時間 -> CMTime ->CMTimeGetSeconds()通過CMTime獲得當前播放時間(單位是秒)
CMTime專門用來表示視頻的播放進度的
value(進度)
timeScale(幀率)
kCMTimeZero 表示初始進度
seekToTime:可以跳轉(zhuǎn)到某一個進度
AVPlayerItem 要播放的音視頻的對象
duration 是CMTime類型 總時長
status 加載的狀態(tài)
AVPlayerItemStatusUnknown, 未知狀態(tài)
AVPlayerItemStatusReadyToPlay, 準備播放狀態(tài)
AVPlayerItemStatusFailed? 失敗狀態(tài)
時間控制的一個類目
currentTime? 獲得當前播放時間
forwardPlaybackEndTime? 跳到結(jié)束位置
reversePlaybackEndTime? 跳到開始位置
seekToTime:
AVPlayerLayer 播放顯示音視頻的圖層界面
AVPlayerViewController? 視圖控制器 可以顯示視頻并且有調(diào)節(jié)控件
使用
AVPlayer直接播放
創(chuàng)建AVPlayerItem 視頻內(nèi)容相關(guān)
創(chuàng)建方式
1、playerItemWithURL:類方法 通過URL地址創(chuàng)建要播放的對象(可以播放本地的內(nèi)容也可以播放在線的內(nèi)容)
2、initWithURL:構(gòu)造方法
3、playerItemWithAsset:通過設備相冊里面的內(nèi)容創(chuàng)建一個對象
4古胆、initWithAsset:
5笋熬、playerItemWithAsset:automaticallyLoadedAssetKeys自動根據(jù)要求的Key去加載相冊里面的內(nèi)容
6危虱、initWithAsset:automaticallyLoadedAssetKeys:
創(chuàng)建AVPlayer
<1>創(chuàng)建方式? 視頻操作相關(guān)
1、playerWithURL:根據(jù)URL去創(chuàng)建播放器 不需使用item2挑豌、initWithURL:3酗失、playerWithPlayerItem:需要傳入一個視頻播放內(nèi)容4义钉、initWithPlayerItem:5、獲得播放結(jié)束的狀態(tài)6规肴、seekToTime跳轉(zhuǎn)到某個進度7捶闸、CMTime:結(jié)構(gòu)體? value(進度) timeScale(幀率)
創(chuàng)建AVPlayerLayer 添加到父視圖
<1>根據(jù)播放器去創(chuàng)建一個可以顯示視頻的圖層playerLayerWithPlayer:->類方法<2>設置它的位置frame是沒有動畫效果的<3>把視頻圖層添加到父圖層
播放 播放器 play
控制器播放
1夜畴、創(chuàng)建AVPlayer
2、創(chuàng)建視頻播放視圖的控制器
3鉴嗤、將創(chuàng)建的AVPlayer賦值給控制器自帶的player
4斩启、跳轉(zhuǎn)到控制器播放
注意:使用控制器播放需要再導入一個AVKit框架
代碼示例
#import"ViewController.h"#import#import@interfaceViewController(){AVPlayer*player;}@end@implementationViewController- (void)viewDidLoad {? ? [superviewDidLoad];UIButton*button = [UIButtonbuttonWithType:UIButtonTypeCustom];? ? button.frame=CGRectMake(100,100,120,100);? ? [button setTitle:@"AVPlayer播放"forState:UIControlStateNormal];? ? [button addTarget:selfaction:@selector(demo1) forControlEvents:UIControlEventTouchUpInside];? ? [self.viewaddSubview:button];UIButton*button1 = [UIButtonbuttonWithType:UIButtonTypeCustom];? ? button1.frame=CGRectMake(240,100,100,100);? ? [button1 setTitle:@"控制器播放"forState:UIControlStateNormal];? ? [button1 addTarget:selfaction:@selector(demo2) forControlEvents:UIControlEventTouchUpInside];? ? [self.viewaddSubview:button1];}#pragma mark ------AVPlayer播放-(void)demo1{/*
視頻播放需要AVPlayer、AVPlayerItem醉锅、AVPlayerLayer
三者的關(guān)系及作用:
AVPlayer(視頻播放器) 去播放 -> AVPlayerItem(視頻播放的元素) -> AVPlayerLayer(展示播放的視圖)
*///1、創(chuàng)建要播放的元素/*
本地的一個視頻
NSString *path = [[NSBundle mainBundle]pathForResource:@"1" ofType:@"m4v"];
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:[NSURL fileURLWithPath:path]];
*/AVPlayerItem*playerItem = [AVPlayerItemplayerItemWithURL:[NSURLURLWithString:@"http://down.treney.com/mov/test.mp4"]];//2发绢、創(chuàng)建播放器player = [AVPlayerplayerWithPlayerItem:playerItem];//3硬耍、創(chuàng)建視頻顯示的圖層AVPlayerLayer*showVodioLayer = [AVPlayerLayerplayerLayerWithPlayer:player];? ? showVodioLayer.frame=self.view.frame;? ? [self.view.layeraddSublayer:showVodioLayer];//4、播放視頻[player play];//獲得播放結(jié)束的狀態(tài) -> 通過發(fā)送通知的形式獲得 ->AVPlayerItemDidPlayToEndTimeNotification[[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(itemDidPlayToEndTime:) name:AVPlayerItemDidPlayToEndTimeNotificationobject:nil];//只要可以獲得到當前視頻元素準備好的狀態(tài) 就可以得到總時長//采取KVO的形式獲得視頻總時長//通過監(jiān)視status 判斷是否準備好 -> 獲得[playerItem addObserver:selfforKeyPath:@"status"options:NSKeyValueObservingOptionNewcontext:nil];}//當status的值改變的時候會調(diào)用這個方法-(void)observeValueForKeyPath:(NSString*)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void*)context{NSLog(@"%@",change[@"new"]);AVPlayerItemStatusstatus = [change[@"new"] integerValue];switch(status) {caseAVPlayerItemStatusUnknown: {NSLog(@"未知狀態(tài)");break;? ? ? ? }caseAVPlayerItemStatusReadyToPlay: {NSLog(@"視頻的總時長%f", CMTimeGetSeconds(player.currentItem.duration));break;? ? ? ? }caseAVPlayerItemStatusFailed: {NSLog(@"加載失敗");break;? ? ? ? }? ? }}//快進-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent*)event{//快進//跳到某一個進度的方法:seekToTime://得到當前的時間 + 快進的時間//獲得當前播放的時間 (秒)Float64 cur =? CMTimeGetSeconds(player.currentTime);? ? cur ++;? ? [player seekToTime:CMTimeMake(cur,1)];}-(void)itemDidPlayToEndTime:(NSNotification*)not{NSLog(@"播放結(jié)束");? ? [player seekToTime:kCMTimeZero];}#pragma mark -----控制器播放-(void)demo2{//1边酒、創(chuàng)建AVPlayer/*
本地視頻
NSURL *url = [[NSBundle mainBundle]URLForResource:@"IMG_9638.m4v" withExtension:nil];
AVPlayer *player = [AVPlayer playerWithURL:url];
*///網(wǎng)頁視頻AVPlayer*player1 = [AVPlayerplayerWithURL:[NSURLURLWithString:@"http://down.treney.com/mov/test.mp4"]];//2经柴、創(chuàng)建視頻播放視圖的控制器AVPlayerViewController*playerVC = [[AVPlayerViewControlleralloc]init];//3、將創(chuàng)建的AVPlayer賦值給控制器自帶的playerplayerVC.player= player1;//4墩朦、跳轉(zhuǎn)到控制器播放[selfpresentViewController:playerVC animated:YEScompletion:nil];? ? [playerVC.playerplay];}@end