Objective-C Tips & Trick - Prioregroup.com
[Objective-C] AVPlayer verify streaming when stalled
       
back

@interface MYPlayer : AVPlayer
{
    id playerObserver;
}

- (void)play;
- (void)stop;
- (void)pause;

@end

@implementation MYPlayer

- (void)play
{
        CMTime interval = CMTimeMake(1, 1);
        playerObserver = [self addPeriodicTimeObserverForInterval:interval queue:dispatch_get_main_queue() usingBlock:^(CMTime time) {
            
            // check stalled 10 sec.
            [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(audioStalled) object:nil];
            [self performSelector:@selector(audioStalled) withObject:nil afterDelay:10.0];
            
            // check end of audio
            CMTime duration = CMTimeConvertScale(self.currentItem.duration, self.currentTime.timescale, kCMTimeRoundingMethod_Default);
            if (CMTIME_COMPARE_INLINE(duration, ==, self.currentTime)) {
	            [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(audioStalled) object:nil];
            }
        }];
        
        [super play];
}

- (void)stop
{
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(audioStalled) object:nil];
    [super pause];
}

- (void)pause
{
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(audioStalled) object:nil];
    [super pause];
}

- (void)audioStalled
{
	// TODO: your code here...
	NSLog("AVPlayer streaming staller!");
}

- (void)dealloc
{
    [self removeTimeObserver:playerObserver];
    playerObserver = nil;

    [NSObject cancelPreviousPerformRequestsWithTarget:self];
}






Copyright © 1996-2018 Centro Studi Informatica di Danilo Priore. All rights reserved. P.I.10149810581.
prioregroup.com