ios avplayer trigger streaming is out of buffer

后端 未结 4 1389
甜味超标
甜味超标 2020-12-07 14:47

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 i

4条回答
  •  Happy的楠姐
    2020-12-07 15:39

    Try this code it should solve all your nightmares:

    #import 
    
    @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;
    }
    

提交回复
热议问题