Xcode Swift:如何停止任何ViewController的背景音乐

问题描述

我正在尝试为每个ViewController播放一首特定的歌曲,该歌曲使用单例开始和停止。我已经创建了课程,音乐开始了。

但是我想在不再需要时通过按下“按钮”在另一个ViewController中停止,因为将启动另一个视图控制器。我无法完成代码调用停止操作。

class MusicHelper {
    static let sharedHelper = MusicHelper()
    var audioPlayer: AVAudioPlayer?

    func playBackgroundMusic() {
        let aSound = NSURL(fileURLWithPath: Bundle.main.path(forResource: "suspense",ofType: "mp3")!)
        do {
            audioPlayer = try AVAudioPlayer(contentsOf:aSound as URL)
            audioPlayer!.numberOfLoops = -1
            audioPlayer!.preparetoPlay()
            audioPlayer!.play()
        } catch {
            print("Cannot play the file")
        }       
 }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)