【问题标题】:Stopping Music in SpriteKit when leaving a GameScene离开 GameScene 时停止 SpriteKit 中的音乐
【发布时间】:2016-12-15 05:14:45
【问题描述】:

我使用 AVFoundation 在我的游戏中实现了背景音乐。但是,当离开 GameScene 时,音乐会继续播放。我知道在 viewControllers 中你可以使用 viewWillDisappear 但我找不到 GameScenes 类似的东西。

var audioPlayer = AVAudioPlayer()
let audioPath = Bundle.main.path(forResource: "electroDrive", ofType: "wav")

//.......//

//.......//

    do {

        try audioPlayer = AVAudioPlayer(contentsOf: URL(fileURLWithPath: audioPath!))

    }
    catch {
        // process error

    }

    audioPlayer.play()


//Code to go to other scene
if viewController != nil {
        self.viewController?.performSegue(withIdentifier: "push", sender: viewController)
    }

【问题讨论】:

  • 你能显示下一个场景的代码吗?
  • 是的,给我一秒钟

标签: ios swift sprite-kit avfoundation


【解决方案1】:

在继续下一个场景之前停止 AVAudioPlayer

在你的 segue 代码之前插入这个

audioPlayer.pause()

【讨论】:

  • 是的。看到你的评论我就明白了。谢谢大佬!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-16
  • 2014-04-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-06
相关资源
最近更新 更多