需要導(dǎo)入系統(tǒng)的 AVFoundation
一累榜、基本使用
(1).AVPlayerItem 播放對象
AVPlayerItem* item = [AVPlayerItem playerItemWithURL:_url];
根據(jù)url可以獲取到播放對象
(2). AVPlayer 播放器對象
AVPlayer * player = [AVPlayerplayerWithPlayerItem:_playerItem];?
根據(jù)item獲取播放器
(3).AVPlayerLayer 播放器圖層
AVPlayerLayer*playerLayer=[AVPlayerLayerplayerLayerWithPlayer:self.player];
根據(jù)播放器獲取播放圖層,AVPlayerLayer 是layer填硕,需要將它添加到view的layer上面顯示
[view.layer addSublayer:playerLayer]
///// 播放和暫停院究、播放速度//////
[player play];// 播放視頻
[player pause]; // 暫停
player.rate=0.1; // 設(shè)置視頻的播放速度
二、視頻播放狀態(tài)
(1)// 視頻播放完的系統(tǒng)通知
[[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(playbackFinished:)name:AVPlayerItemDidPlayToEndTimeNotificationobject:self.player.currentItem];
(2)// 跳轉(zhuǎn)到那個(gè)時(shí)間
[player seekToTime:CMTimeMake(0,1)];
(3)// 時(shí)間變化
AVPlayerItem*playerItem=self.player.currentItem;
//這里設(shè)置每秒執(zhí)行一次
__weaktypeof(self) weakSelf=self;//設(shè)置self的弱應(yīng)用放置block循環(huán)引用
[self.player addPeriodicTimeObserverForInterval:CMTimeMake(1.0,10)queue:dispatch_get_main_queue()usingBlock:^(CMTimetime) {
// 當(dāng)前時(shí)間
float current=CMTimeGetSeconds(time);
// 總得時(shí)間
float total=CMTimeGetSeconds([playerItemduration]);
//NSLog(@"當(dāng)前已經(jīng)播放%.2fs.",current);
}
}];
(4) // kvo 監(jiān)聽 視頻開始狀態(tài) 還有 緩沖進(jìn)度
// 視頻狀態(tài)變化
[_playerItemaddObserver:selfforKeyPath:@"status"options:NSKeyValueObservingOptionNewcontext:nil];
// 視頻緩沖
[_playerItemaddObserver:selfforKeyPath:@"loadedTimeRanges"options:NSKeyValueObservingOptionNewcontext:nil];
// 在這里 實(shí)現(xiàn)
- (void)observeValueForKeyPath:(NSString*)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void*)context
{
if([keyPathisEqualToString:@"status"]) {
NSLog(@"%@",change);
NSIntegerne = [[changeobjectForKey:@"new"]integerValue];
if(ne ==AVPlayerStatusReadyToPlay) {
[_player play];
float total=CMTimeGetSeconds([_playerItemduration]);
}else{
}
}else{
float total=CMTimeGetSeconds([_playerItem duration]);
NSArray*array=_playerItem.loadedTimeRanges;
CMTimeRange timeRange = [array.firstObject CMTimeRangeValue];//本次緩沖時(shí)間范圍
float startSeconds =CMTimeGetSeconds(timeRange.start);
float durationSeconds =CMTimeGetSeconds(timeRange.duration);
NSTimeInterval totalBuffer = startSeconds + durationSeconds;//緩沖總長度
//NSLog(@"共緩沖:%.2f",totalBuffer);
//NSLog(@" %f ~~~~~~~%f",totalBuffer,total);
}
}
}
(5)最后 加上? iOS自定義系統(tǒng)音量