Skip to content

Instantly share code, notes, and snippets.

@priore
Last active February 28, 2016 02:53
Show Gist options
  • Save priore/144ffd00ee15b0ab1454 to your computer and use it in GitHub Desktop.
Save priore/144ffd00ee15b0ab1454 to your computer and use it in GitHub Desktop.
AVPlayer verify streaming when stalled
@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];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment