Reputation: 177
I cannot for the life of me figure out why I can't detect when my sound is done playing. I can get it working for video no problem. I've been searching for the past two hours and none of the solutions I've come across seem to work.
The sound plays fine. I just need to know when it finishes playing.
.h
@interface soundController : UIViewController <AVAudioPlayerDelegate> {
AVAudioPlayer *audioPlayer;
}
- (IBAction) soundButtonClicked;
- (void) soundDidStop:(NSNotification *)notification;
.m
- (IBAction) soundButtonClicked {
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/my_sound.mp3", [[NSBundle mainBundle] resourcePath]]];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
audioPlayer.numberOfLoops = 0;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(soundDidStop:)
name:AVPlayerItemDidPlayToEndTimeNotification
object:audioPlayer];
[audioPlayer play];
}
- (void) soundDidStop:(NSNotification *)notification {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@""
message:@"sound stopped"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
}
Any insight is greatly appreciated. Thanks!
Upvotes: 1
Views: 1989
Reputation: 89569
AVPlayerItemDidPlayToEndTimeNotification` is a notification available to AVPlayerItem, which is not what you're using in your code up there.
You're using AVAudioPlayer, a totally different object with a totally different interface.
And in your case, you can use the AVAudioPlayerDelegate protocol. The delegate method you want to catch is audioPlayerDidFinishPlaying:successfully:
.
Upvotes: 1