0

I have a simple iPhone app with several sounds already playing. However, one in particularly won't. The code block I've hacked up is:

NSString *soundPath2 = [[NSBundle mainBundle] pathForResource:@"gameover" ofType:@"wav"];
NSURL *soundFileURL2 = [NSURL fileURLWithPath:soundPath2];
NSError *err;
AVAudioPlayer *sound2 = [[[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL2 error:&err] autorelease];
NSLog(@"Error: %@", [err description]);
NSLog(@"Duration: %f ", [sound2 duration]);
BOOL b = [sound2 play];
NSLog(@"Play Bool: %d", b);

The console shows the error is null, the boolean returns true, and even the duration is correct! Yet, play does nothing. If I change "gameover" to another sound (a simple beep), all is fine.

While it would be great to get it play, I'm also struggling to understand what error conditions, etc. I can check to 1) know it failed and 2) why.

The sound is question is available at http://wdr1.com/gameover.wav.

4

1 回答 1

4

问题是您不应该自动释放 AVAudioPlayer。play 方法是异步发生的,如果播放器被自动释放,那么没有任何东西可以抓住它,它就会死掉。

一旦播放器在代理中播放完毕,您应该释放音频播放器:

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)audioPlayer successfully:(BOOL)flag
于 2009-11-26T21:20:38.760 回答