Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/107.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Cocoa touch 等待另一个声音结束后再播放新的声音_Cocoa Touch_Ios_Audio - Fatal编程技术网

Cocoa touch 等待另一个声音结束后再播放新的声音

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(

我正在开发一个可以播放声音的iPhone应用程序。我不想打断或播放另一个声音,如果一个声音已经在播放,不管它是从哪里“产生”来播放的。我的代码如下:

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];
你可能想要,你可能想要。