iOS-自定義視頻播放器

采用調(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];

Demo下載

https://pan.baidu.com/s/1i5FInu1

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容