SHARE
TWEET

AVPlayer verify streaming when stalled

priore Oct 29th, 2014 (edited) 655 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. @interface MYPlayer : AVPlayer
  2. {
  3.     id playerObserver;
  4. }
  5.  
  6. - (void)play;
  7. - (void)stop;
  8. - (void)pause;
  9.  
  10. @end
  11.  
  12. @implementation MYPlayer
  13.  
  14. - (void)play
  15. {
  16.         CMTime interval = CMTimeMake(1, 1);
  17.         playerObserver = [self addPeriodicTimeObserverForInterval:interval queue:dispatch_get_main_queue() usingBlock:^(CMTime time) {
  18.            
  19.             // check stalled 10 sec.
  20.             [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(audioStalled) object:nil];
  21.             [self performSelector:@selector(audioStalled) withObject:nil afterDelay:10.0];
  22.            
  23.             // check end of audio
  24.             CMTime duration = CMTimeConvertScale(self.currentItem.duration, self.currentTime.timescale, kCMTimeRoundingMethod_Default);
  25.             if (CMTIME_COMPARE_INLINE(duration, ==, self.currentTime)) {
  26.                     [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(audioStalled) object:nil];
  27.             }
  28.         }];
  29.        
  30.         [super play];
  31. }
  32.  
  33. - (void)stop
  34. {
  35.     [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(audioStalled) object:nil];
  36.     [super pause];
  37. }
  38.  
  39. - (void)pause
  40. {
  41.     [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(audioStalled) object:nil];
  42.     [super pause];
  43. }
  44.  
  45. - (void)audioStalled
  46. {
  47.         // TODO: your code here...
  48.         NSLog("AVPlayer streaming staller!");
  49. }
  50.  
  51. - (void)dealloc
  52. {
  53.     [self removeTimeObserver:playerObserver];
  54.     playerObserver = nil;
  55.  
  56.     [NSObject cancelPreviousPerformRequestsWithTarget:self];
  57. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top