SKAudioNode earphone crash

I've been working with the new SKAudioNode because the previous method of playing short audio with SKAction.playSoundFileNamed() was causing memory leaks on iOS9 (which seems to be already documented on these forums).


            let audio = SKAudioNode(fileNamed: fileName)
            audio.autoplayLooped = false
            node.addChild(audio)
            let action = SKAction.runBlock({ () -> Void in
                let play = SKAction.play()
                audio.runAction(play)
            })
            return action


Above is the snippet of code where I instantiate my SKAudioNode. The audio plays fine but when I plug/unplug earphones and try to play the audio, my app crashes and I get this error:


2016-01-31 01:58:40.485 dash[4860:1360578] 01:58:40.485 ERROR:    [0x19fc2d000] AVAudioPlayerNode.mm:333: Start: required condition is false: _engine->IsRunning()
2016-01-31 01:58:40.486 dash[4860:1360578] *** Terminating app due to uncaught exception 'com.apple.coreaudio.avfaudio', reason: 'required condition is false: _engine->IsRunning()'


This happens everytime I plug or unplug earphones and then try to play a sound with SKAudioNode.


Help greatly appreciated.

Replies

update: found a workaround of adding a check for whether or not audioEngine is running inside my SKAction block, and starting it if audioEngine isn't running. However, plugging/unpugging earphones stopping audioEngine doesn't seem like intended behavior?

Hi rmkz,

I am struggling with the same issue, would it be possible to share the section of the code where you are doing the check for whether or not audioEngine is running and starting if audioEngine is not running.

Best regards,

hi Rmkz and Arda,

i'm facing the same issue when changing output route from default speaker to headphones or headphones to Default speaker. app crash with below description.


ERROR:    [0x1af37fb40] >avae> AVAudioPlayerNode.mm:344: Start: required condition is false: _engine->IsRunning()
*** Terminating app due to uncaught exception 'com.apple.coreaudio.avfaudio', reason: 'required condition is false: _engine->IsRunning()'


I tried to set "audioEngine" start when avaudiosessionRouteChange Notification fire with below code.


func listenForNotifications() {
    NotificationCenter.default.addObserver(self, selector: #selector(handleRouteChange(_:)), name: NSNotification.Name.AVAudioSessionRouteChange, object: nil)
}
func handleRouteChange(_ notification: Notification) {
    guard
        let userInfo = notification.userInfo,
        let reasonRaw = userInfo[AVAudioSessionRouteChangeReasonKey] as? NSNumber,
        let reason = AVAudioSessionRouteChangeReason(rawValue: reasonRaw.uintValue)
        else { fatalError("Strange... could not get routeChange") }
  
    if !self.audioEngine.isRunning {
        do {
            try self.audioEngine.start()
        } catch {
            /
        }
    }
}


Sometimes it's working properly but most of the time getting an error.
did you find any solution for this issue?