Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to tell that AVAudioPlayer has finished playing

I'm developing an app that plays sounds using AVAudioPlayer, and I want to know when a sound has finished playing. I want to change an image when the sound stops playing

Here's the code I use to create the player:

NSURL* url = [[NSBundle mainBundle] URLForResource:@"LOLManFace1"  withExtension:@"mp3"];
NSAssert(url, @"URL is valid.");
NSError* error = nil;
self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&url];error;

if(!self.player)
{
    NSLog(@"Error creating player: %@", error);
}

And here's the code I use to play the sound:

[self.player play];

How can I tell that playback has finished?

Thank you so much in advance!

like image 367
Joel Ferman Avatar asked Dec 20 '22 11:12

Joel Ferman


1 Answers

Look at the AVAudioPlayerDelegate protocol which has a method to tell the delegate when audio playback has finished among other things, specifically what you are looking for is - (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag, here is a reference

Hope it helps

Daniel

like image 156
Daniel Avatar answered Dec 22 '22 01:12

Daniel