迅速地演奏声音而不滞后

时间:2023-01-24 08:26:18

As many developers know, using AVAudioPlayer for playing sound in games can result in jerky animation/movement, because of a tiny delay each time a sound is played.

正如许多开发人员所知,在游戏中使用AVAudioPlayer播放声音会导致动画/移动不流畅,因为每次播放声音都会有微小的延迟。

I used to overcome this in Objective-C, by using OpenAL through a wrapper class (also in Obj-C).

我曾经在Objective-C中通过使用OpenAL(也在object - c中)来克服这个问题。

I now use Swift for all new projects, but I can't figure out how to use my wrapper class from Swift. I can import the class (through a bridging header), but when I need to create ALCdevice and ALCcontext objects in my Swift file, Xcode won't accept it.

我现在对所有新项目都使用Swift,但是我不知道如何使用Swift的包装类。我可以导入类(通过桥接头),但是当我需要在Swift文件中创建ALCdevice和ALCcontext对象时,Xcode不会接受它。

Does anyone have or know of a working example of playing a sound using OpenAL from Swift? Or maybe sound without lag can be achieved in some other way in Swift?

有没有人知道或者知道一个使用OpenAL演奏的例子?或者也许没有延迟的声音可以通过其他方式在Swift中实现?

1 个解决方案

#1


2  

I've ran to a delay-type problem once, I hope your problem is the same one I've encountered.

我曾经遇到过一个延迟类型的问题,我希望你的问题和我遇到的一样。

In my situation, I was using Sprite-Kit to play my sounds, using SKAction.playSoundFileNamed:. It would always lag half a second behind where I wanted it to play.

在我的情况下,我使用sprid - kit来播放我的声音,使用skaction . playsoundfilename:。它总是落后于我想让它发挥作用的那半秒。

This is because it takes time to allocate memory for each SKAction call. To solve this, store the sound action in a variable so you can reuse the sound later without instantiating new objects. It saved me from the delay. This technique would probably work for AVAudioPlayer too.

这是因为为每个SKAction调用分配内存需要时间。要解决这个问题,请将声音动作存储在一个变量中,以便以后可以重用声音,而不必实例化新的对象。它把我从耽搁中救了出来。这种技术可能也适用于AVAudioPlayer。

#1


2  

I've ran to a delay-type problem once, I hope your problem is the same one I've encountered.

我曾经遇到过一个延迟类型的问题,我希望你的问题和我遇到的一样。

In my situation, I was using Sprite-Kit to play my sounds, using SKAction.playSoundFileNamed:. It would always lag half a second behind where I wanted it to play.

在我的情况下,我使用sprid - kit来播放我的声音,使用skaction . playsoundfilename:。它总是落后于我想让它发挥作用的那半秒。

This is because it takes time to allocate memory for each SKAction call. To solve this, store the sound action in a variable so you can reuse the sound later without instantiating new objects. It saved me from the delay. This technique would probably work for AVAudioPlayer too.

这是因为为每个SKAction调用分配内存需要时间。要解决这个问题,请将声音动作存储在一个变量中,以便以后可以重用声音,而不必实例化新的对象。它把我从耽搁中救了出来。这种技术可能也适用于AVAudioPlayer。