我正在开发一个播放声音的 iPhone 应用程序。我不想打断或播放另一个声音,如果一个声音已经在播放,不管它是从哪里“生成”来播放的。我的代码如下:
if(distance <= 10)
{
NSString *soundPath = [[NSBundle mainBundle] pathForResource:@"sound" ofType:@"wav"];
SystemSoundID soundID;
AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath: soundPath], &soundID);
AudioServicesPlaySystemSound(soundID);
}
简单地说,如果一个已经在播放的声音,我不想播放新的声音。
如果一个声音已经在播放,我不想排队播放另一个声音。
这可能吗?
最佳答案
我建议您使用 AVAudioPlayer API 而不是 AudioServicesPlaySystemSound。它有一个您可以使用的委托(delegate),它定义了一个方法 -audioPlayerDidFinishPlaying:successfully: 它将告诉您何时播放完声音。 AVPlayer 类还有一个 -playing 方法,您可以检查当前是否正在播放声音。
所以,换句话说,改为这样做:
NSError *error;
AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundPath error:&error];
if(!player)
[self doSomethingWithError:error];
[player setDelegate:self];
[player play];
关于cocoa-touch - 等到另一种声音结束后再播放新声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5697639/