iosswiftsksceneskaudionode

works when gameScene is loaded first , but play for half a sec and immediately stops (no error) when loaded by another scene, why ?


in the gameScene.swift:
declaration:

var backgroundMusic: SKAudioNode!

in didMoveToView:

  if let musicURL = NSBundle.mainBundle().URLForResource("Jalandhar", withExtension: "mp3") {
            backgroundMusic = SKAudioNode(URL: musicURL)
             addChild(backgroundMusic)
        }

Solution

  • i ended up using AVAudioPlayer to workaround it , code may be useful:
    declare as property :

    var audioPlayer: AVAudioPlayer!
    

    then in a function :

     let filePath = NSBundle.mainBundle().pathForResource("musicfile", ofType: "mp3")
            let url = NSURL.fileURLWithPath(filePath!)
            do {
                audioPlayer = try AVAudioPlayer(contentsOfURL: url, fileTypeHint: nil)
                audioPlayer.play()        }
            catch {
                print("music error")
            }