AVPlayer buffering, pausing notification, and poster frame

后端 未结 1 1348
走了就别回头了
走了就别回头了 2020-12-08 01:17

I have some questions related to AVPlayer which are:

  1. When we pause the AVPlayer through [player pause] does the

相关标签:
1条回答
  • 2020-12-08 02:04

    1) AVPlayer will buffer the video in several cases, none cleary documented. I'd say you can expect buffering when you init the video, and when you replace the current item. You can observe currentItem.loadedTimeRanges to know what's going on. That property will tell you which video time ranges has been loaded.

    Also, there is a few other currentItem properties that may help you: playbackLikelyToKeepUp, playbackBufferFull and playbackBufferEmpty.

    Achieving a perfect gapless playback is not easy.

    /* player is an instance of AVPlayer */
    [player addObserver:self 
             forKeyPath:@"currentItem.loadedTimeRanges" 
                options:NSKeyValueObservingOptionNew 
                context:kTimeRangesKVO];    
    

    In observeValueForKeyPath:ofObject:change:context::

    if (kTimeRangesKVO == context) {
       NSArray *timeRanges = (NSArray *)[change objectForKey:NSKeyValueChangeNewKey];
       if (timeRanges && [timeRanges count]) {
           CMTimeRange timerange = [[timeRanges objectAtIndex:0] CMTimeRangeValue];
           NSLog(@" . . . %.5f -> %.5f", CMTimeGetSeconds(timerange.start), CMTimeGetSeconds(CMTimeAdd(timerange.start, timerange.duration)));
       }
    }
    

    2) Just keep an eye on player.rate.

    [player addObserver:self 
             forKeyPath:@"rate" 
                options:NSKeyValueObservingOptionNew 
                context:kRateDidChangeKVO];
    

    Then in your observeValueForKeyPath:ofObject:change:context::

        if (kRateDidChangeKVO == context) {
            NSLog(@"Player playback rate changed: %.5f", player.rate);
            if (player.rate == 0.0) {
                NSLog(@" . . . PAUSED (or just started)");
            }
        }
    

    3) You can build a movie of a given length using a still image but it's easier to use a regular UIImageView on top of the player. Hide/show it when needed.

    Sample project: feel free to play with the code I wrote to support my answer.

    0 讨论(0)
提交回复
热议问题