Richard G. Nielsen
Richard G. Nielsen

Reputation: 1271

Playing sound without lag in Swift

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.

I used to overcome this in Objective-C, by using OpenAL through a wrapper class (also in Obj-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.

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?

Upvotes: 2

Views: 1719

Answers (1)

Kelvin Lau
Kelvin Lau

Reputation: 6781

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.

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.

Upvotes: 2

Related Questions