cocoa-touch - 等到另一种声音结束后再播放新声音

标签 cocoa-touch ios audio

我正在开发一个播放声音的 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/

相关文章:

c# - 麦克风启动功能卡住了统一应用

javascript - HTML5 混音

iphone - 同时加载行和整个表时崩溃

objective-c - 从 iOS 设备上传图片到服务器

ios - AVFoundation 条形码扫描器在 iphone 上运行良好但在 Ipad 2 上运行不佳

ios - 如何根据collectionView中的条件隐藏textview及其高度?

iphone - 优化 Cocoa/Objective-C 搜索

objective-c - 滚动到底部时向 UITableView 添加行

ios - 从核心数据中获取数据

android - 如何为Android Visualizer产生动态色彩