2011-04-18 101 views
1

我正在开发播放声音的iPhone应用程序。不管中断什么时候“产生”,我都不想中断或播放另一个声音。我的代码如下:等待,直到另一个声音完成之前播放一个新的

if(distance <= 10) 
{ 
    NSString *soundPath = [[NSBundle mainBundle] pathForResource:@"sound" ofType:@"wav"]; 
    SystemSoundID soundID; 
    AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath: soundPath], &soundID); 
    AudioServicesPlaySystemSound(soundID); 
} 

简而言之,我不想播放新的声音,如果一个已经播放。

我不想排队另一个声音播放,如果一个已经播放。

这可能吗?

+0

你可能想[AudioServicesAddSystemSoundCompletion()](http://developer.apple.com/library/ios/documentation/AudioToolbox/Reference/SystemSoundServicesReference /Reference/reference.html#//apple_ref/c/func/AudioServicesAddSystemSoundCompletion)。 – 2011-04-18 01:37:51

回答

4

我建议你使用AVAudioPlayer API而不是AudioServicesPlaySystemSound。它有一个你可以使用的委托,它定义了一个方法-audioPlayerDidFinishPlaying:成功:它会告诉你你的声音什么时候结束。 AVPlayer类也有一个播放方法,您可以查看声音是否正在播放。

所以,换句话说,这样做,而不是:

NSError *error; 
AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundPath error:&error]; 
if(!player) 
    [self doSomethingWithError:error]; 
[player setDelegate:self]; 
[player play]; 
相关问题