iOS: How to play audio without fps drops?
<p>I am in the process of developing a game for iOS 9+ using Sprite Kit and preferably using Swift libraries.</p>

<p>Currently I'm using a Singleton where I preload my audio files, each connected to a separate instance of AVAudioPlayer.</p>

<p>Here's a short code-snipped to get the idea:</p>

<pre><code>import SpriteKit
import AudioToolbox
import AVFoundation

class AudioEngine {

    static let sharedInstance = AudioEngine()

    internal var sfxPing: AVAudioPlayer

    private init() {

        self.sfxPing = AVAudioPlayer()

        if let path = NSBundle.mainBundle().pathForResource("ping", ofType: "m4a") {
            do {
                let url = NSURL(fileURLWithPath:path)
                sfxPing = try AVAudioPlayer(contentsOfURL: url)
                sfxPing.prepareToPlay()
            } catch {
                print("ERROR: Can't load ping.m4a audio file.")
            }
        }

    }

}
</code></pre>

<p>This Singleton is initialised during app start-up. In the game-loop I then just call the following line to play a specific audio file:</p>

<pre><code>AudioEngine.sharedInstance.sfxPing.play()
</code></pre>

<p><strong>This basically works, but I always get glitches when a file is played and the frame rate drops from 60.0 to 56.0 on my iPad Air.</strong></p>

<p>Someone any idea how to fix this performance issue with AVAudioPlayer ?</p>

<p>I also watched out for 3rd party libraries, namely:</p>

<ul>
<li><a href="http://audiokit.io/features/" rel="nofollow">AudioKit</a> [Looks very heavy-weighted]</li>
<li><a href="http://kstenerud.github.io/ObjectAL-for-iPhone/" rel="nofollow">ObjectAL</a> [Last Update 2013 ...]</li>
<li><a href="https://developer.apple.com/library/ios/documentation/AVFoundation/Reference/AVAudioEngine_Class/" rel="nofollow">AVAudioEngine</a> [Based on AVAudioPlayer, same problems ?]</li>
</ul>

<p><strong>Requirements:</strong></p>

<ul>
<li>Play a lot of very short samples (like shots, hits, etc..)</li>
<li>Play some motor effects (thus pitching would be nice)</li>
<li>Play some background / ambient sound in a loop</li>
<li>NO nasty glitches / frame rate drops !</li>
</ul>

<p>Could you recommend any of the above mentioned libraries for my requirements or point out the problems using the above code ?</p>

<p><strong>UPDATE:</strong></p>

<p>Playing short sounds with:</p>

<pre><code>self.runAction(SKAction.playSoundFileNamed("sfx.caf", waitForCompletion: false))
</code></pre>

<p>does indeed improve the frame rate. <a href="https://permadi.com/2009/05/exporting-audio-file-to-caf/" rel="nofollow">I exported the audio files with Audiacity to the .caf format (Apple's Core Audio Format).</a> But in the tutorial, they export with "<code>Signed 32-bit PCM</code>" encoding which led to disturbed audio playback in my case. Using any of the other encoding options (<code>32-bit float</code>, <code>U-Law</code>, <code>A-Law</code>, etc..) worked fine for me.</p>

<p>Why using <code>caf</code> format? Because it's uncompressed and thus loaded faster into memory with less CPU overhead compared to compressed formats like <code>m4a</code>. For short sound effects played a lot in short intervals, this makes sense and disk usage is not affected much for short audio files consuming few kilobytes. For bigger audio files, like ambient and background music, using compressed formats (<code>mp3</code>, <code>m4a</code>) is obviously the better choice.</p>