Advertisement
priore

How to check the end of an audio (AVPlayer)

Sep 29th, 2014
1,228
0
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.     }];
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement