Thread 1: EXC_BAD_ACCESS

Hi,


I just installed an Xcode update and now my game app freezes when I build it and has a debug error statement:


Thread 1: EXC_BAD_ACCESS(code=1, address=0x48)


on the AVAudioPlayer line:


do {backgroundAudio = try AVAudioPlayer(contentsOf: audioNSURL as URL) }


Here is the complete code in GameViewController that used to work fine:


Import SpriteKit

Import GameplayKit

Import AVFoundation


class GameViewController: UIViewController {


var backgroundAudio = AVAudioPlayer()


override func viewDidLoad() {


super.viewDidLoad()


let filePath = Bundle.main.path(forResource: “MySong”, ofType: “wav”)

let audioNSURL = NSURL(fileURLWithPath: filePath!)


do {backgroundAudio = try AVAudioPlayer(contentsOf: audioNSURL as URL) }

catch {return print(“Cannot Find The Audio”)}


backgroundAudio.numberOfLoops = -1

backgroundAudio.volume = 0.7

background.play()


}



What has happened and how can I fix this?

Replies

I did some research and found the solution courtesy to Ray Wenderlich and Co. by creating an non-optional var and func with string parameter:


Import SpriteKit

Import GameplayKit

Import AVFoundation


class GameViewController: UIViewController {


var backgroundAudio = AVAudioPlayer!


override func viewDidLoad() {

super.viewDidLoad()

playBackgroundAudio(audioname: “MySong.wav”)

}//vDL


func playBackgroundAudio(audioname: String) {

let urlPath = Bundle.main.url(forResource: audioname, withExtension: nil)

guard let url = urlPath else {print(“Could not find file: \(audioname)”)

return

}

do {

try backgroundAudio = AVAudioPlayer(contentsOf: url)

backgroundAudio.numberOfLoops = -1

backgroundAudio.volume = 0.7

background.play()

}

catch {print(“Cannot Find The Audio”)

return

}

}//func


}//ClassGVC


But I still don’t understand why the old code stopped working when Xcode updated.