ios avplayer trigger streaming is out of buffer

18,052

Solution 1

you can add observer for those keys:

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

The first one will warn you when your buffer is empty and the second when your buffer is good to go again.

Then to handle the key change you can use this code:

- (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
        }
    }
}

Solution 2

You will need to drop down into Core Audio and CFReadStream to do this. WIth CFReadStream, you can provide a callback that gets called on certain stream events like end encountered, read error, etc. From there you can trigger a reconnect to the server. If you're consuming an HTTP stream, you can add the range header to the HTTP request so you can tell the server to send the stream from a point you specify (which would be the last byte you received before + 1).

Solution 3

Try this code it should solve all your nightmares:

#import <AVFoundation/AVFoundation.h>

@interface CustomAVPlayerItem : AVPlayerItem
{
    BOOL bufferEmptyVideoWasStopped;
}

-(void)manuallyRegisterEvents;

@end

=========== in the .m file

#import "CustomAVPlayerItem.h"
#import "AppDelegate.h"

@implementation CustomAVPlayerItem

-(void)manuallyRegisterEvents
{
    //NSLog(@"manuallyRegisterEvents %lu", (unsigned long)self.hash);

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

    bufferEmptyVideoWasStopped=NO;
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if (object == self && [keyPath isEqualToString:@"playbackBufferEmpty"])
    {
        if (self.playbackBufferEmpty)
        {
            //NSLog(@"AVPLAYER playbackBufferEmpty");
            bufferEmptyVideoWasStopped=YES;
            [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_VIDEO_PLAYBACKBUFFER_EMPTY object:self];
        }
    }
    else if (object == self && [keyPath isEqualToString:@"playbackLikelyToKeepUp"])
    {
        if (self.playbackLikelyToKeepUp)
        {
            //NSLog(@"AVPLAYER playbackLikelyToKeepUp");

            if(bufferEmptyVideoWasStopped)
            {
                bufferEmptyVideoWasStopped=NO;

                [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_VIDEO_PLAYBACKBUFFER_FULL object:self];
            }
            else
                [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_VIDEO_PLAYBACKBUFFER_KEEP_UP object:self];

        }
    }
}

-(void)dealloc
{
    //NSLog(@"dealloc CustomAVPlayerItem %lu", (unsigned long)self.hash);

    [self removeObserver:self forKeyPath:@"playbackBufferEmpty"];
    [self removeObserver:self forKeyPath:@"playbackLikelyToKeepUp"];
}

@end

===== and now where you want to use it

-(void)startVideoWithSound
{
    if([CustomLog jsonFieldAvailable:[videoObject objectForKey:@"video_url"]])
    {
        CustomAVPlayerItem *playerItem=[[CustomAVPlayerItem alloc] initWithURL:[[OfflineManager new] getCachedURLForVideoURL:[videoObject objectForKey:@"video_url"]]];

        AVPlayer *player=[AVPlayer playerWithPlayerItem:playerItem];
        [playerItem manuallyRegisterEvents];
        [player play];

        player.muted=NO;

        [viewPlayer setPlayer:player];

        viewPlayer.hidden=NO;

        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notifBufferEmpty:) name:NOTIFICATION_VIDEO_PLAYBACKBUFFER_EMPTY object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notifBufferFull:) name:NOTIFICATION_VIDEO_PLAYBACKBUFFER_FULL object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notifBufferKeepUp:) name:NOTIFICATION_VIDEO_PLAYBACKBUFFER_KEEP_UP object:nil];
    }
}

- (void)notifBufferEmpty:(NSNotification *)notification
{
    //NSLog(@"notifBufferEmpty");

    [[viewPlayer player] play]; // resume it
    viewLoading.hidden=NO;
}

- (void)notifBufferFull:(NSNotification *)notification
{
    //NSLog(@"notifBufferFull");
    viewLoading.hidden=YES;
}

- (void)notifBufferKeepUp:(NSNotification *)notification
{
    //NSLog(@"notifBufferKeepUp");
    viewLoading.hidden=YES;
}
Share:
18,052
Admin
Author by

Admin

Updated on June 08, 2022

Comments

  • Admin
    Admin almost 2 years

    I want to reconnect to the server when the streaming buffer is empty.

    How can I trigger a method when the AVPlayer or AVPlayerItem buffer is empty?

    I know there are playbackLikelyToKeepUp, playbackBufferEmpty and playbackBufferFull methods to check the buffer status, but those are not callbacks.

    Are there any callback functions, or any observers I should add?

  • Andrew Kuklewicz
    Andrew Kuklewicz over 12 years
    Thanks! Do you know any good way to make the AVPlayer or AVPlayerItem reconnect again, or am I left with creating a new player or item?
  • sciasxp
    sciasxp over 12 years
    In my case I just needed to send play message to my AVPlayer object.
  • pronebird
    pronebird almost 11 years
    You don't wanna do this. Trust me.
  • Ruben Martinez Jr.
    Ruben Martinez Jr. over 8 years
    Does AVPlayer outright give up at some point during buffering? Because I've noticed sometimes connection slows, and no attempt to reconnect seems to be happening after a while, in which case manually stopping+starting again fixes things. Would be nice to detect this programmatically.
  • Ruben Martinez Jr.
    Ruben Martinez Jr. over 8 years
    RE above: when I've noticed this happen, it often coincides with the "playbackBufferEmpty" key-value observer NOT being hit.