【问题标题】:Playing sound effect with no latency in Objective-C在 Objective-C 中无延迟播放音效
【发布时间】:2013-01-04 18:31:28
【问题描述】:

当人们在我的应用程序中按下某些按钮时,我想播放一些简单的声音效果,我已经尝试了几件事,但我总是遇到延迟,使声音看起来不同步。

我已经按照教程here 进行了操作,因此我尝试了音频服务中的构建,但它有延迟,我尝试了 AVAudioPlayer,但它也有延迟,即使我使用了“ prepareToPlay”。

我真的必须安装像Finch 这样的大而杂乱的库才能在我的简单应用中获得没有延迟的简单音效吗?

希望你能帮我澄清一下!

更新

音频服务代码:

NSURL *soundURL = [[NSBundle mainBundle] URLForResource:@"PlopsX4"
                                      withExtension:@"aif"];
AudioServicesCreateSystemSoundID((CFURLRef)soundURL, &sound1);
AudioServicesPlaySystemSound(sound1);

viewDidLoad 中 AVAudioPlayer 的代码:

NSURL *soundURL = [[NSBundle mainBundle] URLForResource:@"PlopsX4"
                                      withExtension:@"aif"];
self.avSound = [[AVAudioPlayer alloc] 
       initWithContentsOfURL:soundURL error:nil];
[self.avSound prepareToPlay]

我要播放文件的方法中的 AVAudioPlayer 代码:

[self.avSound play];

【问题讨论】:

  • 愚蠢的问题,但你确定你的音频文件的开头没有片刻的沉默吗?您是否经历过诸如大胆之类的事情?
  • 另外,你是如何测试的?设备?模拟器?如果它在设备上,您正在测试什么硬件?
  • 你搜索过吗。在这个问题上似乎还有很多其他问题。一些建议预加载一些 hack,其他建议使用系统声音 (SystemSoundID)
  • @DanF 开始时没有片刻的沉默。我自己发出了声音。第二次播放,立即播放……这只是第一次。我正在测试连接到我的 macbook air 的最新一代 iPod touch。
  • @BjörnKaiser 刚刚做了。会喜欢上面的一些cmets。

标签: iphone objective-c ios audio ios6


【解决方案1】:

我让这个工作的方式(我想我从另一篇关于 SO 的帖子中得到这个想法)是创建一个空的 1 秒 .wav 文件并在初始化时“播放”它。之后,我调用其他音效就没有延迟了。

我的声音播放器对象中有这样的东西

    SystemSoundID blID;
    SystemSoundID cID;

    +(void)doInit
    {
            if (spinitialized){
                    AudioServicesPlaySystemSound (blID);
                    return;
            }



            NSString *str = [[NSBundle mainBundle] pathForResource:@"ding.wav" ofType:@""];
            NSURL *uref = [NSURL fileURLWithPath:str];
            OSStatus error = AudioServicesCreateSystemSoundID ((CFURLRef)uref, &cID);
            if (error) NSLog(@"SoundPlayer doInit Error is %ld",error);

            str = [[NSBundle mainBundle] pathForResource:@"blank.wav" ofType:@""];
            uref = [NSURL fileURLWithPath:str];
            error = AudioServicesCreateSystemSoundID ((CFURLRef)uref, &blID);
            if (error) NSLog(@"SoundPlayer doInit Error is %ld",error);

            AudioServicesPlaySystemSound (blID);

            spinitialized = YES;
    }

然后我可以毫不拖延地使用:

    AudioServicesPlaySystemSound (cid);

【讨论】:

  • 感谢您的建议。我做了类似的事情(见下文)。
【解决方案2】:

解决方案

我最终做了类似于上面迈克的事情。我最终在 viewDidLoad 中降低音量播放声音:

NSURL *soundURL = [[NSBundle mainBundle] URLForResource:@"PlopsX4"
                                              withExtension:@"aif"];
self.plops = [[AVAudioPlayer alloc] initWithContentsOfURL:soundURL error:nil];
[self.plops setVolume:0.0];
[self.plops play];

然后在我播放它的函数中:

[self.plops setVolume:1.0];
[self.plops play];

【讨论】:

    【解决方案3】:

    我发现似乎有帮助的一件事是,在进入可能播放声音的屏幕时,播放声音的一小部分。这似乎“启动”了声音机制——分配内部对象、分页等。

    (我观察到这类似于 Mike M 的方法。)

    【讨论】:

      猜你喜欢
      • 2015-10-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-09
      • 1970-01-01
      • 2012-06-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多