Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @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];
- }
Add Comment
Please, Sign In to add comment