Using AVAudioPlayer and AVAudioEngine on multiple threads

I'd like to move some operations involving AVAudioPlayer - and possibly AVAudioEngine, although that's less important - off the main thread using a serial dispatch queue or similar mechanism. Here's a simple self-contained example of what I'm talking about:


import AVFoundation
import UIKit

class ViewController: UIViewController {
    let player = try! AVAudioPlayer(
        contentsOf: Bundle.main.resourceURL!.appendingPathComponent("audio.m4a")
    )
    let queue = DispatchQueue(label: "", qos: .userInteractive)

    override func touchesBegan(_ touches: Set, with event: UIEvent?) {
        queue.async {
            if self.player.isPlaying {
                self.player.stop()
                self.player.currentTime = 0.0
            } else {
                self.player.play()
            }
        }
    }
}


In this example, an AVAudioPlayer instance is created on the main thread, and then subsequent operations are performed on other threads using a serial queue.


Is it safe to use AVAudioPlayer, or related tools like AVAudioEngine, in this way? Again, all operations on these objects would be serial, never concurrent.

Using AVAudioPlayer and AVAudioEngine on multiple threads
 
 
Q