AVPlayer實現(xiàn)簡單播放視頻

播放視頻以前我們可以使用MPMoviePlayerController,雖然MP很簡單,但是不能定制UI,并且很多功能不能實現(xiàn),AVFoundation中的AVPlayer應(yīng)運而生,首先我們來看一幅圖:

AVPlayer.png

實現(xiàn)一個簡單的網(wǎng)絡(luò)視頻播放器,需要注意三個重要對象:
(1)AVPlayer:負責播放視頻
(2)AVPlayerItem:負責管理視頻數(shù)據(jù),apple給出的API解釋:A player item manages the presentation state of an asset with which it is associated. A player item contains player item tracks—instances of [AVPlayerItemTrack](https://developer.apple.com/library/mac/documentation/AVFoundation/Reference/AVPlayerItemTrack_Class/Reference/Reference.html#//apple_ref/occ/cl/AVPlayerItemTrack)—that correspond to the tracks in the asset
(3) AVPlayerLayer:視頻播放界面顯示圖層
廢話不多說,直接上代碼:

static void *PlayViewStatusObservationContext = &PlayViewStatusObservationContext;

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [_player pause];
    _player = nil;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    AVPlayerItem *item = [[AVPlayerItem alloc] initWithURL:[NSURL URLWithString:@"http://static.tripbe.com/videofiles/20121214/9533522808.f4v.mp4"]];

//播放狀態(tài)
    [item addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew context:PlayViewStatusObservationContext];
    //緩沖總時間
    [item addObserver:self forKeyPath:@"loadedTimeRanges" options:NSKeyValueObservingOptionNew context:PlayViewStatusObservationContext];
    // 緩沖區(qū)空了,需要等待數(shù)據(jù)
    [item addObserver:self forKeyPath:@"playbackBufferEmpty" options: NSKeyValueObservingOptionNew context:PlayViewStatusObservationContext];
    // 緩沖區(qū)有足夠數(shù)據(jù)可以播放了
    [item addObserver:self forKeyPath:@"playbackLikelyToKeepUp" options: NSKeyValueObservingOptionNew context:PlayViewStatusObservationContext];

    _player = [[AVPlayer alloc] initWithPlayerItem:item];
    AVPlayerLayer *layer = [AVPlayerLayer playerLayerWithPlayer:_player];
    layer.frame = self.view.bounds;
    layer.contentsScale = [UIScreen mainScreen].scale;
    layer.videoGravity = AVLayerVideoGravityResizeAspectFill;
    [self.view.layer addSublayer:layer];

    _currentTime = [[UILabel alloc] initWithFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height - 20, 120, 20)];
    _currentTime.textAlignment = 1;
    _currentTime.textColor = [UIColor whiteColor];
    _currentTime.backgroundColor = [UIColor blackColor];
    [self.view addSubview:_currentTime];
    
    _slider = [[UISlider alloc] initWithFrame:CGRectMake(120, [UIScreen mainScreen].bounds.size.height - 20, [UIScreen mainScreen].bounds.size.width - 240, 5.0)];
    [self.view addSubview:_slider];

    
    _totalTime = [[UILabel alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width  - 120, _slider.frame.origin.y, 120, 20)];
    _totalTime.textAlignment = 1;
    _totalTime.textColor = [UIColor whiteColor];
    _totalTime.backgroundColor = [UIColor blackColor];
    [self.view addSubview:_totalTime];
    
    _totalTime.text = [self toTimeStrWithSeconds:CMTimeGetSeconds(_player.currentItem.duration)];

    [_slider addTarget:self action:@selector(slideAction:) forControlEvents:UIControlEventValueChanged];
    //播放器添加定時器,CMTimtMake(a,b)相當于a/b秒就會進入這個block
    [_player  addPeriodicTimeObserverForInterval:CMTimeMake(1.0, 1.0)
                                           queue:dispatch_get_main_queue()
                                      usingBlock:^(CMTime time) {
                                          float currentValue = CMTimeGetSeconds(time);
                                          float totalValue = CMTimeGetSeconds(item.duration);
                                          _totalTime.text = [self toTimeStrWithSeconds:totalValue];
                                          _currentTime.text = [self toTimeStrWithSeconds:currentValue];
                                          [_slider setValue: currentValue / totalValue];
                                      }];
    
    //播放進度相關(guān)
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(playtoEndAction:) name:AVPlayerItemDidPlayToEndTimeNotification object:_player];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(jumpedTimeAction:) name:AVPlayerItemTimeJumpedNotification object:_player];
    
    //播放后臺相關(guān)
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(applicationDidBackground:)
                                                 name:UIApplicationDidEnterBackgroundNotification object:_player];

注意,在這里添加KVO操作的是AVPayerItem對象而不是AVPlayer對象,注意在dealloc里面移除KVO,在KVO方法里面可以獲取Item的一些數(shù)據(jù),比如播放狀態(tài)、緩沖數(shù)據(jù)大小等等,這對于我們定制播放界面UI很有幫助,像開發(fā)者可以自己定制進度條,獲取播放進度、獲取緩沖進度等等:

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
    if ([object isKindOfClass:[AVPlayerItem class]]) {
        AVPlayerItem *item =(AVPlayerItem *)object;
        if ([keyPath rangeOfString:@"status"].length) {
            //播放狀態(tài)
            if (item.status == AVPlayerStatusReadyToPlay) {
                [_player play];
            } else if (item.status == AVPlayerStatusFailed) {
                NSLog(@"失敗!");
            } else if (item.status == AVPlayerStatusUnknown) {
                NSLog(@"未知!");
            }
        } else if ([keyPath rangeOfString:@"loadedTimeRanges"].length) {
            //緩沖
            NSArray *caches = item.loadedTimeRanges;
            CMTimeRange range = [caches.firstObject CMTimeRangeValue];
            float startSeconds = CMTimeGetSeconds(range.start);
            float durationSeconds  = CMTimeGetSeconds(range.duration);
            float cachesSeconds =  startSeconds + durationSeconds;
            NSString *subStr = @"%";
            float totalDuration = CMTimeGetSeconds(item.duration);
            NSLog(@"共緩沖了%@%.2f",subStr,cachesSeconds / totalDuration * 100.0);
            
        }
    }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

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

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