Skip to content

Instantly share code, notes, and snippets.

@priore
Created November 12, 2014 11:22
Show Gist options
  • Save priore/d45ffd2e9ea02c0d2f6e to your computer and use it in GitHub Desktop.
Save priore/d45ffd2e9ea02c0d2f6e to your computer and use it in GitHub Desktop.
How to check the end of an audio (AVPlayer)
// How to check the end of an audio
AVAsset *asset = [AVURLAsset URLAssetWithURL:your-url options:nil];
AVPlayerItem *anItem = [AVPlayerItem playerItemWithAsset:asset];
self.player = [AVPlayer playerWithPlayerItem:anItem];
__weak typeof(self) wself = self;
CMTime interval = CMTimeMake(1, 1);
id observer = [self.soundPlayer addPeriodicTimeObserverForInterval:interval queue:dispatch_get_main_queue() usingBlock:^(CMTime time) {
// controlla la fine dell'audio
CMTime duration = CMTimeConvertScale(wself.player.currentItem.duration, wself.player.currentTime.timescale, kCMTimeRoundingMethod_Default);
if (CMTIME_COMPARE_INLINE(duration, ==, wself.player.currentTime)) {
// TODO: your code here
}
}];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment