如何解决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 举报,一经查实,本站将立刻删除。