2011-04-18 98 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]; 
相關問題