Cocoa touch 等待另一个声音结束后再播放新的声音
我正在开发一个可以播放声音的iPhone应用程序。我不想打断或播放另一个声音,如果一个声音已经在播放,不管它是从哪里“产生”来播放的。我的代码如下:Cocoa touch 等待另一个声音结束后再播放新的声音,cocoa-touch,ios,audio,Cocoa Touch,Ios,Audio,我正在开发一个可以播放声音的iPhone应用程序。我不想打断或播放另一个声音,如果一个声音已经在播放,不管它是从哪里“产生”来播放的。我的代码如下: if(distance <= 10) { NSString *soundPath = [[NSBundle mainBundle] pathForResource:@"sound" ofType:@"wav"]; SystemSoundID soundID; AudioServicesCreateSystemSoundID(
if(distance <= 10)
{
NSString *soundPath = [[NSBundle mainBundle] pathForResource:@"sound" ofType:@"wav"];
SystemSoundID soundID;
AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath: soundPath], &soundID);
AudioServicesPlaySystemSound(soundID);
}
if(距离我建议您使用AVAudioPlayer API而不是AudioServicesPlaySystemSound。它有一个委托,您可以使用它定义一个方法-audioPlayerDidFinishPlaying:successfully:它将告诉您声音何时结束播放。AVPlayer类还有一个-playing方法,您可以检查声音是否为currentl我在玩
因此,换言之,要这样做:
NSError *error;
AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundPath error:&error];
if(!player)
[self doSomethingWithError:error];
[player setDelegate:self];
[player play];
我建议您使用AVAudioPlayer API,而不是AudioServicesPlaySystemSound。它有一个委托,您可以使用该委托定义一个方法-AudioPlayerIDFinishPlaying:successfully:它将告诉您声音何时结束播放。AVPlayer类还有一个-playing方法,您可以检查当前是否播放声音。
因此,换言之,要这样做:
NSError *error;
AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundPath error:&error];
if(!player)
[self doSomethingWithError:error];
[player setDelegate:self];
[player play];
你可能想要,你可能想要。