AVPlayer playback issues, pause and buffering

My application is playing streaming video, but when it is buffered the player goes into pause mode and I have to set it to play mode again manually, I have the following code in my AVPlayer class to handle this situation, but it doesn't work.

In the ViewDidLoad method

[playerItem addObserver:self forKeyPath:@"playbackBufferEmpty" options:NSKeyValueObservingOptionNew context:nil];
[playerItem addObserver:self forKeyPath:@"playbackLikelyToKeepUp" options:NSKeyValueObservingOptionNew context:nil];

      

and then referring to observers using the following methods

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object
                    change:(NSDictionary *)change context:(void *)context {
if (!player)
{
    return;
}

else if (object == playerItem && [keyPath isEqualToString:@"playbackBufferEmpty"])
{
    if (playerItem.playbackBufferEmpty) {
        //Your code here
    }
}

else if (object == playerItem && [keyPath isEqualToString:@"playbackLikelyToKeepUp"])
{
    if (playerItem.playbackLikelyToKeepUp)
    {
        //Your code here
    }
}

      

}

is there any other solution to this problem to force the player to keep playing?

+3


source to share


1 answer


This can help,

suppose this is your AVPlayer object
  player1 = [AVPlayer playerWithURL: streamURL];

When your video is buffered, you can pause it and when it will play again, for example: In the observer method



if ([object isKindOfClass:[AVPlayerItem class]])
{
    AVPlayerItem *item = (AVPlayerItem *)object;
    //playerItem status value changed?
    if ([keyPath isEqualToString:@"status"])
    {   //yes->check it...

        NSLog(@"STATUS = %d",item.status);
        switch(item.status)
        {
            case AVPlayerItemStatusFailed:
                NSLog(@"player item status failed");
                break;
            case AVPlayerItemStatusReadyToPlay:

                 [playButton setTitle:@"Pause" forState:UIControlStateNormal];
                [player1 play];

                NSLog(@"player item status is ready to play");
                break;
            case AVPlayerItemStatusUnknown:
                NSLog(@"player item status is unknown");
                break;
        }
    }
    else if ([keyPath isEqualToString:@"playbackBufferEmpty"])
    {
        if (item.playbackBufferEmpty)
        {
            [playButton setTitle:@"Play" forState:UIControlStateNormal];
            [player1 pause];
            NSLog(@"player item playback buffer is empty");
        }
    }
}

      

or you can save this event with a button click. Place one button on the screen to support play and pause and addTarget with OnClick event.

+1


source







All Articles