Ios 我的mp3赢了';返回后,使用SKAudioNode和Swift在SpriteKit场景中正确播放

Ios 我的mp3赢了';返回后,使用SKAudioNode和Swift在SpriteKit场景中正确播放,ios,swift,audio,skaudionode,Ios,Swift,Audio,Skaudionode,所以基本上,我在第一个场景中播放音乐,一切正常。我切换到场景2,音乐自动停止,这就是我想要的。但随后我转回来,音乐开始了半秒钟。。然后无缘无故地被切断。只是为了让事情变得更奇怪,只有当我将我的物理设备置于睡眠模式,然后解锁它时,它才会再次完美地继续 我做了一个过于简化的代码,只是为了直接解决我在实际项目中遇到的问题。这是我的第一个场景,它只是在启动时播放音乐,触摸后过渡到场景2: import SpriteKit class GameScene: SKScene { override

所以基本上,我在第一个场景中播放音乐,一切正常。我切换到场景2,音乐自动停止,这就是我想要的。但随后我转回来,音乐开始了半秒钟。。然后无缘无故地被切断。只是为了让事情变得更奇怪,只有当我将我的物理设备置于睡眠模式,然后解锁它时,它才会再次完美地继续

我做了一个过于简化的代码,只是为了直接解决我在实际项目中遇到的问题。这是我的第一个场景,它只是在启动时播放音乐,触摸后过渡到场景2:

import SpriteKit

class GameScene: SKScene
{
    override func didMoveToView(view: SKView)
    {
        let backgroundMusic = SKAudioNode(fileNamed: "WhatIsLove.mp3")
        backgroundMusic.autoplayLooped = true
        backgroundMusic.positional = false
        addChild(backgroundMusic)
    }
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?)
    {
        let transition = SKTransition.revealWithDirection(.Down, duration: 1.0)
        let nextScene = SceneTwo(size: scene!.size)
        nextScene.scaleMode = .AspectFill
        nextScene.backgroundColor = UIColor.blueColor()
        scene?.view?.presentScene(nextScene, transition: transition)
    }
}
导入SpriteKit
类游戏场景:SKScene
{
覆盖func didMoveToView(视图:SKView)
{
让backgroundMusic=SKAudioNode(文件名为:“WhatIsLove.mp3”)
backgroundMusic.autoplayloped=true
backgroundMusic.positional=false
addChild(背景音乐)
}
覆盖功能触摸开始(触摸:设置,withEvent事件:UIEvent?)
{
let transition=SKTransition.revealWithDirection(.Down,持续时间:1.0)
让nextScene=SceneTwo(大小:scene!.size)
nextScene.scaleMode=.AspectFill
nextScene.backgroundColor=UIColor.blueColor()
场景?.view?.presentScene(下一个场景,过渡:过渡)
}
}
这里是场景2,音乐在进入之前自动停止。除了触摸后切换回场景1之外,这里没有其他内容,我希望音乐再次启动(但半秒钟后停止播放):

导入SpriteKit
课堂场景:SKScene
{
覆盖func didMoveToView(视图:SKView)
{
}
覆盖功能触摸开始(触摸:设置,withEvent事件:UIEvent?)
{
let transition=SKTransition.revealWithDirection(.Down,持续时间:1.0)
让nextScene=游戏场景(大小:scene!.size)
nextScene.scaleMode=.AspectFill
nextScene.backgroundColor=UIColor.greenColor()
场景?.view?.presentScene(下一个场景,过渡:过渡)
}
}

不知道为什么会这样。我尝试了几种与SKAudioNode的组合以及其他方法来声明变量并实现它。我尝试过不使用AutoPlayLoop和positional选项,而且几乎所有选项都使用过。当我切换回第一个场景时,我希望音乐能再次正常播放。

我也有同样的问题。代码这么简单,真让人讨厌。我也有同样的问题。代码这么简单,真让人讨厌。
import SpriteKit

class SceneTwo: SKScene
{
    override func didMoveToView(view: SKView)
    {
    }

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?)
    {
        let transition = SKTransition.revealWithDirection(.Down, duration: 1.0)
        let nextScene = GameScene(size: scene!.size)
        nextScene.scaleMode = .AspectFill
        nextScene.backgroundColor = UIColor.greenColor()
        scene?.view?.presentScene(nextScene, transition: transition)
    }
}