I’m attempting to play a sound using AVAudioPlayer. Should be simple, but I’m seeing some odd results.
Code:
NSString *path = [[NSBundle mainBundle] pathForResource:@"pop" ofType:@"wav"];
NSURL *url = [NSURL fileURLWithPath:path];
AVAudioPlayer *sound = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
[sound play];
[sound release];
What I’m seeing is that the sound doesn’t play when using the app normally.
It plays only if I step through the code using the debugger, it doesn’t play when executing any other way…
I’m not creating any new threads or run loops within my app, so this should all be running on the main thread, at least [NSThread isMainThread] returns true.
Anyone got any ideas as to what is going on here?
AVAudioPlayer’s
playmethod is asynchronous, so you’re starting to play the sound and then immediately releasing it! That’s why it works when you step through it in the debugger — you’re giving it time to play before killing it. What you want to do is implement AVAudioPlayerDelegate’s –(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flagmethod and release the audio player there, after the sound is done playing.