tags:

views:

346

answers:

2

I'm trying to get a simple button to play a sound:

-(IBAction)myButton:(id)sender {
    NSString *audioFilePath = [[NSBundle mainBundle] pathForResource:@"whistle" ofType:@"wav"];
    AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:audioFilePath] error:NULL];  
    [audioPlayer play];     
}

Using the debugger, the call to 'play' never returns. No sound is played either. The button stays in the highlighted state until I hit the home button to exit the app.

From the about menu of the simulator I'm using 'Version 2.2 (77.4.9)'.

-Bill

A: 

Could it be a bad sound file? What happens if instead of -play you call -prepareToPlay?

mahboudz
Same behavior. Double-clicking the .wav file in Xcode results in it playing in iTunes.
Bill
I just tried your code, and it works fine in a sample app made from Xcode's View Based App Template.Now, you aren't releasing the audioPlayer are you? The only place that would be safe to release it is either in an AVAudioPlayer call-back or when your app is quitting (well, actually it would be safe to release it when you know for sure that the sound has already played). -autorelease has the same issues since your sound will play after the runloop has completed and the autorelease pool has been released.
mahboudz
If you're not releasing, the other place I would check is whether commenting out [audioPlayer play] would still end up hanging your app.Since play queues up the actual playback for later and returns almost immediately, you may have something else leaving your app in a weird state.
mahboudz
A: 

I don't know if it's the same problem, but AVAudioPlayer was causing my 2.2.x simulator to crash, too. In my case it turns out a plugin inside /Library/QuickTime/ (specifically, DivX Decoder.component) was messing with the simluator. I just moved the file temporarily to my desktop, and then the simulator started working again for me.

Shaggy Frog