线程1:播放声音时,EXC_BAD_ACCESS(代码= 1,地址= 0x48)
问题描述:
每当我尝试将声音文件分配给变量时,我的应用程序就会崩溃.我已经仔细检查了我的声音文件名,但无法正常工作. 这是我的代码:
Whenever I try to assign a sound file to a variable my app crashes. I've double checked my sound file names, but I can't get it to work. Here is my code:
struct variables {
static var randomCode = 0
static var timerCounter = 30
static var timer = Timer()
static var explosion:AVAudioPlayer = AVAudioPlayer()
static var defuseSound:AVAudioPlayer = AVAudioPlayer()
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
//Error occurs on the following line
let explosionFile = Bundle.main.path(forResource: "explosion", ofType: ".mp3")
let defuseFile = Bundle.main.path(forResource: "defused", ofType: ".mp3")
do {
try variables.explosion = AVAudioPlayer(contentsOf: URL(fileURLWithPath: explosionFile!))
try variables.defuseSound = AVAudioPlayer(contentsOf: URL(fileURLWithPath: defuseFile!))
} catch {
print(error)
}
答
已多次回答,将其分配给变量或常量时,AVAudioPlayer不再接受参数.
This was answered many times, the AVAudioPlayer doesnt take arguments anymore when assigning it to a variable or constant.
static var explosion : AVAudioPlayer?
static var defuseSound : AVAudioPlayer?
// OR static var explosion : AVAudioPlayer! = nil