我在Swift 3.0中制作了一个带有关卡选择的游戏,并且我使用AVFoundation播放背景音乐。我想为每个级别播放不同的歌曲,但是当我尝试在击败关卡后停止播放并播放新歌曲时,不是停止播放,而是重播它。这是我的代码,认真地在这个问题上工作了几个小时而且没有在哪里。
import UIKit
import AVFoundation
class LevelSelect: UIViewController {
var AudioPlayer0 = AVAudioPlayer()
override func viewDidLoad() {
super.viewDidLoad()
let AssortedMusics0 = NSURL(fileURLWithPath: Bundle.main.path(forResource: "Background", ofType: "mp3")!)
AudioPlayer0 = try! AVAudioPlayer(contentsOf: AssortedMusics0 as URL)
AudioPlayer0.prepareToPlay()
AudioPlayer0.numberOfLoops = -1
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
@IBAction func Level0Button(_ sender: Any) {
AudioPlayer0.stop()
AudioPlayer0.play()
}
}
感谢您的帮助!