等到另一种声音是打新的前完成 [英] Waiting until another sound is finished before playing a new one

查看:172
本文介绍了等到另一种声音是打新的前完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发一个播放声音的iPhone应用程序。我不想打断或播放另一种声音,如果声音已在播放,不管它是催生从哪里玩。我的code是如下:

I am developing an iPhone application that plays sounds. I do not want interrupt or play another sound, if a sound is already playing, regardless of where it was "spawned" from to play. My code is as follows:

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

简单地说,我并不想打一个新的声音如果正在播放。

Simply, I do not want to play a new sound if one is already playing.

我不想排队另一个声音播放如果正在播放。

I do not want to queue another sound to play if one is already playing.

这可能吗?

推荐答案

我建议你使用AVAudioPlayer API,而不是AudioServicesPlaySystemSound。它有一个委托,您可以使用,它定义了一个方法-audioPlayerDidFinishPlaying:成功:它会告诉你,当你的声音播放完毕。该AVPlayer类还有一个方法-playing,你可以检查,看是否有声音正在播放。

I suggest you use the AVAudioPlayer API rather than AudioServicesPlaySystemSound. It has a delegate which you can use, which defines a method -audioPlayerDidFinishPlaying:successfully: which will tell you when your sound finishes playing. The AVPlayer class also has a -playing method which you can check to see if a sound is currently playing.

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

So, in other words, do this instead:

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

这篇关于等到另一种声音是打新的前完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆