在Objective-C中播放没有延迟的声音效果 [英] Playing sound effect with no latency in Objective-C

查看:211
本文介绍了在Objective-C中播放没有延迟的声音效果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to play some simple sound effects when people press certain buttons in my app and I've tried several things, but I always get a latency that makes the sound seem out of sync.

我已经按照这里的教程,所以我尝试了内置版本音频服务,但有延迟,我尝试了AVAudioPlayer,但也有延迟,即使我使用prepareToPlay。

I've followed the tutorials here, so I've tried the build in Audio Services, but that had a latency and I've tried the AVAudioPlayer, but that had a latency as well, even though I used "prepareToPlay".

我真的我必须在 Finch 上安装一个庞大且凌乱的库,以便在我的简单应用程序中获得简单的声音效果且没有延迟?

Do I really have to install a big and messy library like Finch to get a simple sound effect with no latency in my simple app?

希望你能为我澄清一切!

Hope you can clarify things for me!

UPDATE

音频服务代码:

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

viewDidLoad中AVAudioPlayer的代码:

Code for AVAudioPlayer in viewDidLoad:

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

AVAudioPlayer的代码,我想播放的方法file:

Code for AVAudioPlayer in method where I want to play the file:

[self.avSound play];


推荐答案

我开始工作的方式(我认为我从另一篇文章得到的想法就是创建一个空的1秒.wav文件并在初始化时播放它。之后,当我调用其他声音效果时没有延迟。

The way I got this to work (and I think I got the idea from another post on SO) was to create an empty 1 second .wav file and "play" it at initialization. After that, there was no delay when I called the other sound effects.

在我的声音播放器对象中有类似的东西

Something like this in my sound player object

    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;
    }

然后我可以毫不拖延地播放ding:

Then I could just play the "ding" without any delay using:

    AudioServicesPlaySystemSound (cid);

这篇关于在Objective-C中播放没有延迟的声音效果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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