无法从游戏场景,Swift 3/Spritekit中停止背景音乐 [英] Cannot stop background music from within Game Scenes, Swift 3/Spritekit

查看:81
本文介绍了无法从游戏场景,Swift 3/Spritekit中停止背景音乐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在XCODE 8/Swift 3和Spritekit上,我正在播放背景音乐(5分钟的歌曲),从GameViewController的ViewDidLoad(来自所有场景的父级,而不是来自特定GameScene)调用它,如我所愿播放整个场景而无需停止.这没有问题.

On XCODE 8/Swift 3 and Spritekit, i am playing background music (a 5 minute song), calling it from GameViewController's ViewDidLoad (from the parent of all the scenes, not from a specific GameScene), as I want it to play throughout scene changes without stopping. This happens without a problem.

但是我的问题是,当我进入场景时,如何随意停止背景音乐?说用户何时在第三场景的游戏中获得特定分数?由于我无法访问父文件的方法.这是我用来播放音乐的代码:

But my problem is, how do i stop the background music at will, when I am inside a scene? Say when user gets to a specific score on the game on the 3rd scene? As i cannot access the methods of the parent file. Here is the code I used to call the music to play:

GameViewController类:UIViewController {

class GameViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()

    var audioPlayer = AVAudioPlayer()

    do {
        audioPlayer =  try AVAudioPlayer(contentsOf: URL.init(fileURLWithPath: Bundle.main.path(forResource: "music", ofType: "mp3")!))
        audioPlayer.prepareToPlay()

    } catch {

        print (error)
    }
    audioPlayer.play()

非常感谢您的帮助

推荐答案

为什么不创建可以从任何地方访问的音乐帮助器类.单例方式或带有静态方法的类.这也应该使您的代码更整洁,更易于管理.

Why not create a music helper class that you can access from anywhere. Either the singleton way or a class with static methods. This should also make your code cleaner and easier to manage.

我还将设置方法和播放方法分开,这样您就不必在每次播放文件时都设置播放器.

I would also split the setup method and the play method so that you do not set up the player each time you play the file.

例如Singleton

e.g Singleton

class MusicManager {

    static let shared = MusicManager()

    var audioPlayer = AVAudioPlayer()


    private init() { } // private singleton init


    func setup() {
         do {
            audioPlayer =  try AVAudioPlayer(contentsOf: URL.init(fileURLWithPath: Bundle.main.path(forResource: "music", ofType: "mp3")!))
             audioPlayer.prepareToPlay()

        } catch {
           print (error)
        }
    }


    func play() {
        audioPlayer.play()
    }

    func stop() {
        audioPlayer.stop()
        audioPlayer.currentTime = 0 // I usually reset the song when I stop it. To pause it create another method and call the pause() method on the audioPlayer.
        audioPlayer.prepareToPlay()
    }
}

项目启动时,只需调用设置方法

When your project launches just call the setup method

MusicManager.shared.setup()

比在项目中的任何地方都可以说

Than from any where in your project you can say

MusicManager.shared.play()

播放音乐.

要停止它,只需调用stop方法

To than stop it just call the stop method

MusicManager.shared.stop()

有关具有更多曲目的功能更丰富的示例,请查看我在GitHub上的帮助程序

For a more feature rich example with multiple tracks check out my helper on GitHub

https://github.com/crashoverride777/SwiftyMusic

希望这会有所帮助

这篇关于无法从游戏场景,Swift 3/Spritekit中停止背景音乐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆