Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS

David Bauer
David Bauer
3,863 Points

After upgrading to XCode7.2 SKAudioNode won't play

I have a problem after upgrading to XCode 7.2 that a SKAudioNode just plays like one second and then stops playing. I changed nothing in the code.

In my GameViewController I call the MenuScene like:

class GameViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()

        let scene = MenuScene(size: view.bounds.size)
        let skView = view as! SKView

        skView.showsFPS = true
        skView.showsNodeCount = true
        skView.ignoresSiblingOrder = true

        scene.scaleMode = .ResizeFill
        skView.presentScene(scene)
    }...

And in my MenuScene I call my GameScene like this:

func launchScene() {
        let gameView = view! as SKView
        let gameScene = GameScene(size: self.size)

        gameView.ignoresSiblingOrder = true

        let reveal = SKTransition.fadeWithDuration(0.5)

        gameView.presentScene(gameScene, transition:reveal)
    }

And then in my GameScene I add a SKAudioNode:

class GameScene: SKScene, SKPhysicsContactDelegate {
    override func didMoveToView(view: SKView) {
    ...
    let backgroundMusic = SKAudioNode(fileNamed: "main.mp3")
    backgroundMusic.autoplayLooped = true
    addChild(backgroundMusic)
    ...
    }
}

So, the problem is that when I hit a Button that calls the launchScene() function the background Music starts playing but stops playing after approximately 1 second.

It seems that the backgroundMusic starts playing before! the transition to the other scene begins and when the other scene (gameScene) is "finally there" (dunno how to describe it) the music stops playing. I don't know why since I add the backgroundMusic in the gameScene in the "didMoveToView" function. Another weird thing is when I reduce the SKTransition.fadeWithDuation to (0) it works. And I don't know why.

What am I doing wrong here since it works flawlessly in XCode 7.1?