导入UIKit 导入AVFoundation
类BackgroundViewController:UIViewController { var audioPlayer = AVAudioPlayer()
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = .white
playBackgroundStartMusic()
}
override func viewWillAppear(_ animated: Bool) {
audioPlayer.stop()
}
//MARK: - Background Music
func playBackgroundStartMusic() {
do {
audioPlayer = try AVAudioPlayer(contentsOf: URL.init(fileURLWithPath: Bundle.main.path(forResource: "IntroMusic", ofType: "wav")!))
audioPlayer.prepareToPlay()
audioPlayer.play()
audioPlayer.numberOfLoops = -1
}
catch {
print(error)
}
}
}
答案 0 :(得分:0)
如果您遇到音乐叠加视图更改的问题,只需将AVFoundation中的audioPlayer.stop()放在您的按钮上即可显示下一个屏幕,这样背景音乐将停止播放而不再播放。