Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SKAudioNode() crashes when plugging in/out headphones

I am using a SKAudioNode() to play background music in my game. I have a play/pause function and everything is working fine until I plug in my headphones. There is no sound at all and when I call the pause/play function I get this error

AVAudioPlayerNode.mm:333: Start: required condition is false: _engine->IsRunning() com.apple.coreaudio.avfaudio', reason: 'required condition is false: _engine->IsRunning()

Does anyone knows what this means?

Code:

import SpriteKit

class GameScene: SKScene {

let loop = SKAudioNode(fileNamed: "gameloop.mp3")
let play = SKAction.play()
let pause = SKAction.pause()
var isPlaying = Bool()

override func didMoveToView(view: SKView) {  
    loop.runAction(play)
    isPlaying = true
    self.addChild(loop)
}

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
    _ = touches.first as UITouch!

    for _ in touches {
        if isPlaying {
            loop.runAction(pause)
            isPlaying = false
        } else {
            loop.runAction(play)
            isPlaying = true
        } 
    }
}
}
like image 529
Cherrypig Avatar asked Feb 26 '16 21:02

Cherrypig


People also ask

Why are my headphones plugged in but no sound coming out?

When left malfunctioning or outdated, sound card drivers can cause PC audio troubles. If after setting your headphones as your default audio device, your “headphones plugged in but sound coming from speakers” issue still persists, try updating your PC’s sound card driver.

Why does my game crash when I turn off my headphones?

It's just sometimes I unplug my headphones by accident and the game crashes. that could be your problem as windows doesnt find any audio output device. what you can do is maybe attach a broken pair of earphones on the external speaker side and your actual headphones on the headphone jack port.

What do I do if my headphones are not working?

Try plugging in your headphones onto another device. If your headphones work, your other audio device may be causing the problem. Alternatively, you can also plug in another set of headphones onto your device’s audio port to see if it works. Restart your audio source

Why can’t I hear sound through my headphones on Windows 10?

If you have multiple audio output devices connected to your Windows 10 PC, your device might stream the sound in the wrong output even after you plug in your headphones. To prevent this from happening, set your headphones as your PC’s default audio device. Follow these steps: Look for the sound icon on the bottom-right corner of your PC’s screen.


2 Answers

I was not able fix it, but by using AVAudioPlayer() I found a good workaround. Thank you all for supporting me!

import SpriteKit
import AVFoundation

class GameScene: SKScene {    
    var audioPlayer = AVAudioPlayer()  

    override func didMoveToView(view: SKView) {        
        initAudioPlayer("gameloop.mp3")        
    }

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {        
        _ = touches.first as UITouch!
        for _ in touches {
            toggleBackgroundMusic()            
        }        
    }

    func initAudioPlayer(filename: String) {        
        let url = NSBundle.mainBundle().URLForResource(filename, withExtension: nil)
        guard let newURL = url else {
            print("Could not find file: \(filename)")
            return
        }
        do {
            audioPlayer = try AVAudioPlayer(contentsOfURL: newURL)
            audioPlayer.numberOfLoops = -1
            audioPlayer.prepareToPlay()
            audioPlayer.play()
        } catch let error as NSError {
            print(error.description)
        }        
    }

    func toggleBackgroundMusic() {        
        if audioPlayer.playing {
            audioPlayer.pause()
        } else {
            audioPlayer.play()
        }        
    }    
}
like image 57
Cherrypig Avatar answered Oct 20 '22 00:10

Cherrypig


SKScene has a property named audioEngine that is stopped in certain circumstances. If you´re using ReplayKit for example, playing a recorded gameplay movie in the RPPreviewController hi-jacks the audio and stops it, so when you dismiss it, the audioEngine is no longer running.

I had this issue and solved it by doing a simple check and starting the audioEngine again. Try this:

if !self.audioEngine.isRunning {
    do {
        try self.audioEngine.start()
    } catch {
        //handle error
    }
}
like image 32
Lucifer Avatar answered Oct 20 '22 00:10

Lucifer