与AVAudioPlayer iPhone上的奇怪行为 [英] Strange behaviour with AVAudioPlayer on the iPhone
问题描述
我试图用打一个AVAudioPlayer的声音。应该是简单的,但我看到了一些奇怪的结果。
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.
它发挥的仅如果我使用调试器通过code步骤,执行其它的方式,当它不玩...
It plays only if I step through the code using the debugger, it doesn't play when executing any other way...
我没有创造我的应用程序中的任何新主题或运行循环,所以这个应该都在主线程上运行,至少 [NSThread isMainThread]
返回true
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的播放
方法是异步的,所以你开始播放声音,然后立即释放它!这就是为什么当你通过它在调试步骤作品 - 你给它时间杀死它之前玩。你想要做的是落实AVAudioPlayerDelegate的 - (无效)audioPlayerDidFinishPlaying:(AVAudioPlayer *)成功的球员:(BOOL)标志
方法并释放音频播放器出现后,声音做游戏。
AVAudioPlayer's play
method 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)flag
method and release the audio player there, after the sound is done playing.
这篇关于与AVAudioPlayer iPhone上的奇怪行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!