这是我的代码:
let laFlame = URL(fileURLWithPath: Bundle.main.path(forResource: "laflame", ofType: "mp3")!)
var audioPlayer = AVAudioPlayer()
@IBAction func laFlame(_ sender: AnyObject) {
do {
audioPlayer = try AVAudioPlayer(contentsOf: laFlame)
audioPlayer.play()
} catch {
// Could not load file.
}
}
我没有错误,但每当按下按钮时,没有任何反应,声音也没有播放。我做错了什么,我该如何解决?
你应该尝试这种方法而不是AVAudioPlayer(contentsOf:)
。
@IBAction func laFlame(_ sender: AnyObject) {
do {
audioPlayer = try AVAudioPlayer(contentsOf: laFlame, fileTypeHint: AVFileType.mp3.rawValue)
audioPlayer.play()
} catch let error {
print(error.localizedDescription)
}
}
希望能帮助到你!