SHARE
TWEET

How to check the end of an audio (AVPlayer)

priore Sep 29th, 2014 (edited) 586 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // How to check the end of an audio
  2. AVAsset *asset = [AVURLAsset URLAssetWithURL:your-url options:nil];
  3. AVPlayerItem *anItem = [AVPlayerItem playerItemWithAsset:asset];
  4. self.player = [AVPlayer playerWithPlayerItem:anItem];
  5.    
  6.     __weak typeof(self) wself = self;
  7.     CMTime interval = CMTimeMake(1, 1);
  8.     id observer = [self.soundPlayer addPeriodicTimeObserverForInterval:interval queue:dispatch_get_main_queue() usingBlock:^(CMTime time) {
  9.         // controlla la fine dell'audio
  10.         CMTime duration = CMTimeConvertScale(wself.player.currentItem.duration, wself.player.currentTime.timescale, kCMTimeRoundingMethod_Default);
  11.         if (CMTIME_COMPARE_INLINE(duration, ==, wself.player.currentTime)) {
  12.             // TODO: your code here
  13.         }
  14.     }];
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