简体   繁体   English

如何调试为什么无法通过AVAudioPlayer播放声音?

[英]How to debug why a sound won't play via AVAudioPlayer?

I have a simple iPhone app with several sounds already playing. 我有一个简单的iPhone应用程序,已经在播放几种声音。 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! 控制台显示错误为null,布尔值返回true,甚至持续时间也是正确的! 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. 虽然发挥作用会很不错,但我也正在努力了解什么错误情况,等等。我可以检查1)知道失败,2)原因。

The sound is question is available at http://wdr1.com/gameover.wav . 有关声音的问题,请访问http://wdr1.com/gameover.wav

The problem is that you shouldn't be autoreleasing the AVAudioPlayer. 问题在于您不应该自动释放AVAudioPlayer。 The play method happens asynchronously and if the player gets autoreleased then nothing is holding on to it and it will just die. play方法是异步发生的,如果播放器被自动释放,则没有任何东西可以保留它,它只会死掉。

You should release your audio player once the player is done playing in the delegate: 播放器在代理中完成播放后,应释放音频播放器:

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)audioPlayer successfully:(BOOL)flag

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM