采用調(diào)用蘋果自帶的AVPlayer來實現(xiàn),但還是支持大部分格式的視頻
主要功能:
1.視頻播放,暫停
2.視頻緩沖,視頻播放狀態(tài)
3.監(jiān)聽播放進度
4.監(jiān)聽播放完成
一.播放視頻
1.將網(wǎng)絡或者本地視頻轉(zhuǎn)化為NSURL
2.加載到播放器
self.playerItem = [AVPlayerItem playerItemWithURL:url];
self.player = [AVPlayer playerWithPlayerItem:self.playerItem];
3.創(chuàng)建播放視圖
AVPlayerLayer *playerLayer=[AVPlayerLayer playerLayerWithPlayer:self.player];
playerLayer.frame = view.bounds;
playerLayer.videoGravity=AVLayerVideoGravityResizeAspect;//視頻填充模式
[view.layer addSublayer:playerLayer];
4.調(diào)用播放/暫停方法
- (void)start {
[self.player play];
}
- (void)pause {
[self.player pause];
}
二.視頻緩沖,視頻播放狀態(tài)
1.通過kvo監(jiān)聽AVPlayerItem的屬性status和loadedTimeRanges
[playerItem addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew context:&PlayerItemStatusContext];
//監(jiān)控網(wǎng)絡加載情況屬性
[playerItem addObserver:self forKeyPath:@"loadedTimeRanges" options:NSKeyValueObservingOptionNew context:&PlayerItemLoadedTimeRangesContext];
2.根據(jù)屬性的改變判斷做相應處理
AVPlayerItemStatusReadyToPlay為正常播放狀態(tài)
3.loadedTimeRanges屬性就是加載的數(shù)據(jù)可以用來計算緩沖進度
NSTimeInterval timeInterval = [self availableDuration];// 計算緩沖進度
CMTime duration = self.playerItem.duration;
CGFloat totalDuration = CMTimeGetSeconds(duration);
//緩沖進度------------
self.toolBarView.playerProgress.progress = timeInterval / totalDuration;
//時間算法
- (NSTimeInterval)availableDuration {
NSArray *loadedTimeRanges = [[_player currentItem] loadedTimeRanges];
CMTimeRange timeRange = [loadedTimeRanges.firstObject CMTimeRangeValue];// 獲取緩沖區(qū)域
float startSeconds = CMTimeGetSeconds(timeRange.start);
float durationSeconds = CMTimeGetSeconds(timeRange.duration);
NSTimeInterval result = startSeconds + durationSeconds;// 計算緩沖總進度
return result;
}
三.監(jiān)聽播放進度
使用NSTimer執(zhí)行updateProgressInfo方法
- (void)updateProgressInfo{
if (!self.isPlaying) return;
// 1.更新時間
self.toolBarView.timeLabel.text = [self updateTimeString];
double currentTime = CMTimeGetSeconds(self.player.currentTime);
// 2.設置進度條的value
self.toolBarView.playerSlider.value = currentTime;
}
#pragma mark - 講當前時間轉(zhuǎn)成時間戳給label顯示
- (NSString*)updateTimeString
{
NSTimeInterval currentTime = CMTimeGetSeconds(self.player.currentItem.currentTime);
NSTimeInterval duration = CMTimeGetSeconds(self.player.currentItem.duration);
return [self stringWithCurrentTime:currentTime duration:duration];
}
- (NSString *)stringWithCurrentTime:(NSTimeInterval)currentTime duration:(NSTimeInterval)duration
{
NSInteger dMin = duration / 60;
NSInteger dSec = (NSInteger)duration % 60;
NSInteger cMin = currentTime / 60;
NSInteger cSec = (NSInteger)currentTime % 60;
NSString *durationString = [NSString stringWithFormat:@"%02ld:%02ld", dMin, dSec];
NSString *currentString = [NSString stringWithFormat:@"%02ld:%02ld", cMin, cSec];
return [NSString stringWithFormat:@"%@/%@", currentString, durationString];
}
四.監(jiān)聽播放完成
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playerEndPlay:) name:AVPlayerItemDidPlayToEndTimeNotification object:self.playerItem];