一.基本概念
1.AVPlayer
AVPlayer是一個(gè)用來(lái)播放的對(duì)象,支持播放本地,分步下載,或者通過(guò)HLS協(xié)議得到的流媒體.
它是一個(gè)不可見(jiàn)的組件,如果播放mp3類的音頻文件,可以.但是如果要想播放視頻文件,我們就需要了解另外一個(gè)類AVPlayerLayer
AVPlayerLayer
它是對(duì)于CALayer類的擴(kuò)展,通過(guò)框架在屏幕上顯示內(nèi)容,作為視頻的渲染面,然后在用戶面前進(jìn)行展示.在創(chuàng)建AVPlayerLayer時(shí),同時(shí)需要一個(gè)指向 AVPlayer的指針,把兩者聯(lián)系在一起.
2.AVPlayerItem
An AVPlayerItem carries a reference to an AVAsset as well as presentation settings for that asset.
This class is intended to represent presentation state for an asset that's played by an AVPlayer and to permit observation of that state.
說(shuō)白了,AVPlayerItem是一個(gè)載體,承載AVAsset,然后通過(guò)AVPlayer進(jìn)行播放.我們要想對(duì)一個(gè)資源進(jìn)行播放,那么就要通過(guò)AVPlayerItem和AVPlayerItemTrack來(lái)構(gòu)建對(duì)應(yīng)的動(dòng)態(tài)內(nèi)容
二.基本使用
2.1 初始化一個(gè)簡(jiǎn)單的播放器
通過(guò)下面一段簡(jiǎn)單的代碼,來(lái)進(jìn)行一個(gè)視頻的播放
NSURL *assetUrl = [NSURL URLWithString:@"http://flv2.bn.netease.com/videolib3/1606/23/RiTxE9164/SD/RiTxE9164-mobile.mp4"];
AVAsset *asset = [AVAsset assetWithURL:assetUrl];
AVPlayerItem *item = [AVPlayerItem playerItemWithAsset:asset];
self.player = [AVPlayer playerWithPlayerItem:item];
AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.player];
playerLayer.frame = CGRectMake(0, 50, 375, 200);
[self.view.layer addSublayer:playerLayer];
2.2 AVPlayerItem的status屬性
雖然現(xiàn)在已經(jīng)初始化完畢,但是視頻還沒(méi)有播放,此時(shí)我們需要對(duì)AVPlayerItem的一個(gè)名為<code>"status"</code>的AVPlayerItemStatus類型的屬性進(jìn)行監(jiān)聽(tīng)
當(dāng)創(chuàng)建之初,AVPlayerItem的status狀態(tài)為<code>AVPlayerItemStatusUnknown</code>,該狀態(tài)標(biāo)識(shí)當(dāng)前媒體還沒(méi)有載入并不在播放隊(duì)列中,當(dāng)媒體加載完畢后,status狀態(tài)會(huì)發(fā)生改變,會(huì)變成<code>AVPlayerItemStatusReadyToPlay</code>或者<code>AVPlayerItemStatusFailed</code>.
當(dāng)狀態(tài)為<code>AVPlayerItemStatusReadyToPlay</code>時(shí),表示視頻加載完畢,可以進(jìn)行播放
下面是添加監(jiān)聽(tīng)的代碼
先定一一個(gè)全局變量
<code>static NSString * const PlayerItemStatusContext = @"PlayerItemStatusContext";</code>
對(duì)item進(jìn)行監(jiān)聽(tīng)
[item addObserver:self forKeyPath:@"status" options:0 context:(__bridge void * _Nullable)(PlayerItemStatusContext)];
然后通過(guò)調(diào)用<code>observeValueForKeyPath: ofObject: change: context:</code>方法做監(jiān)聽(tīng)回調(diào)
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context{
if (context == (__bridge void * _Nullable)(PlayerItemStatusContext)) {
if ([keyPath isEqualToString:@"status"]) {
AVPlayerItem * item = (AVPlayerItem *)object;
if (item.status == AVPlayerItemStatusReadyToPlay) { //準(zhǔn)備好播放
[self.player play];
}else if (item.status == AVPlayerItemStatusFailed){ //失敗
NSLog(@"failed");
}
}
}
}
2.3 loadedTimeRanges狀態(tài)
通常情況下,在加載網(wǎng)絡(luò)視頻時(shí),我們需要獲取視頻的緩沖進(jìn)度,這時(shí)候,我們可以通過(guò)監(jiān)聽(tīng)AVPlayerItem的<code>loadedTimeRanges</code>狀態(tài),獲取緩沖進(jìn)度
定義一個(gè)全局變量
static NSString * const PlayerPreloadObserverContext = @"PlayerPreloadObserverContext";
為item添加監(jiān)聽(tīng)
[item addObserver:self forKeyPath:@"loadedTimeRanges" options:(NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld) context:(__bridge void * _Nullable)(PlayerPreloadObserverContext)];
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context{
if (context == (__bridge void * _Nullable)(PlayerPreloadObserverContext)){
if ([keyPath isEqualToString:@"loadedTimeRanges"]) {
NSArray *timeRanges = (NSArray *)[change objectForKey:NSKeyValueChangeNewKey];
[self updateLoadedTimeRanges:timeRanges];
}
}
}
- (void)updateLoadedTimeRanges:(NSArray *)timeRanges {
if (timeRanges && [timeRanges count]) {
CMTimeRange timerange = [[timeRanges firstObject] CMTimeRangeValue];
CMTime bufferDuration = CMTimeAdd(timerange.start, timerange.duration);
// 獲取到緩沖的時(shí)間,然后除以總時(shí)間,得到緩沖的進(jìn)度
NSLog(@"%f",CMTimeGetSeconds(bufferDuration));
}
}
2.4移除監(jiān)聽(tīng)
在移除播放器的時(shí)候,一定要記得移除監(jiān)聽(tīng)事件
- (void)removeObserverWithPlayerItem:(AVPlayerItem *)playerItem {
[playerItem removeObserver:self forKeyPath:XHPlayerStatusKey context:(__bridge void *)XHPlayerItemObserverContext];
[playerItem removeObserver:self forKeyPath:XHPlayerPlayerLoadedTimeRanges context:(__bridge void *)XHPlayerPreloadObserverContext];
}