我使用以下代码播放音频:
let url = Bundle.main.url(forResource: "0", withExtension: "mp3")!
do {
audioPlayer = try AVAudioPlayer(contentsOf: url)
audioPlayer.delegate = self
audioPlayer.prepareToPlay()
play(sender:AnyObject.self as AnyObject)
} catch {
}
在iPhone X和iPhone 7 Plus的iOS 12.4.1上,此代码可以正常工作。但是在iOS 13及更高版本上,我的应用程序在此行audioPlayer = try AVAudioPlayer(contentsOf: url)
崩溃,错误为Thread 1: EXC_BAD_ACCESS (code=1, address=0x48)
。但是在我的模拟器上,带有iOS 13的iPhone 11 Pro Max都可以正常工作。为什么会发生这种情况以及如何解决?
答案 0 :(得分:0)
我也有此错误,您可能是这样实例化audioPlayer的:
let audioPlayer = AVAudioPlayer ()
尝试创建一个可选的:
let audioPlayer: AVAudioPlayer?
并在viewDidLoad()中完成代码:
let path = Bundle.main.path (forResource: "0", ofType: "mp3")!
let url = URL(fileURLWithPath: path)
do {
audioPlayer = try AVAudioPlayer(contentsOf: url)
} catch{
}
答案 1 :(得分:0)
您可以像这样实例化音频播放器:
var audioPlayer : AVAudioPlayer!
然后请编写此代码:
do {
self.audioPlayer = try AVAudioPlayer(contentsOf: sound)
self.audioPlayer.play()
}catch {}
这对我有用。