denz10jr
denz10jr

Reputation: 31

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

I am using swift on XCODE. I am running into this problem where if I want to click on my button to play my sound. It will crash. I have viewcontrollers. The first viewcontroller is connected to One storyboard. That story board has a button play sound. When I run it, the sound will play. When it goes to the second storyboard to play sound it crashes

[The 2 pictures are the code I used to play the audio. I have the same code pasted on the first viewcontroller.] This is what is on the second viewcontroller. I get the Thread 1: EXC_BAD_ACCESS (code=1, address=0x48) when I press the button on the second storyboard but it doesn't get this error on the first button I press.

It happens when I run the simulator and get to the second view controller

First

Second

class Level1ViewController: UIViewController, AVAudioPlayerDelegate {

    var player: AVAudioPlayer = AVAudioPlayer()
    var correctPlayer: AVAudioPlayer = AVAudioPlayer()
    var wrongPlayer: AVAudioPlayer = AVAudioPlayer()




    @IBAction func correctAnswer(_ sender: Any) {
        correctPlayer.play()
    }

    @IBAction func playOne(_ sender: Any) {
        player.play()
    }
    @IBOutlet weak var option2: UIButton! // wrong answer

    @IBAction func wrongAnswer1LV(_ sender: Any) {
    }


    @IBOutlet weak var option1: UIButton!

    override func viewDidLoad() {
        super.viewDidLoad()


        //View Controller 1 - Working

        do {
            let audioPlayer = Bundle.main.path (forResource: "bark", ofType: "wav")

            try player = AVAudioPlayer(contentsOf: NSURL(fileURLWithPath: audioPlayer!)as URL)
        }

        catch {
            //ERROR!
        }

        do {
            let audioPlayer2 = Bundle.main.path (forResource: "Ding-Sound-Effect_qZC5gtOw3DU", ofType: "mp3")

            try correctPlayer = AVAudioPlayer(contentsOf: NSURL(fileURLWithPath: audioPlayer2!)as URL)
        }

        catch {
            //ERROR!
        }

View Controller 2 - Doesn't work (gives error)

class Level2LevelViewController: UIViewController, AVAudioPlayerDelegate {

    var player: AVAudioPlayer = AVAudioPlayer()
    var correctPlayer: AVAudioPlayer = AVAudioPlayer()
    var wrongPlayer: AVAudioPlayer = AVAudioPlayer()

    @IBAction func levelTwoPLay(_ sender: Any) {
        correctPlayer.play()  **** THREAD 1: EXC_BAD_ACESS****
    }



    override func viewDidLoad() {
        super.viewDidLoad()


        do {
            let audioPlayerII = Bundle.main.path (forResource: "Door Open and Close Slam Sound Effects", ofType:"mp3")

            try player = AVAudioPlayer(contentsOf: NSURL(fileURLWithPath: audioPlayerII!)as URL)
        }

        catch {
            //ERROR!!
        }

    leveltwoOp1.backgroundColor = UIColor.black
    leveltwoOp2.backgroundColor = UIColor.black
    leveltwoOpTh.backgroundColor = UIColor.black
    leveltwoOp4.backgroundColor = UIColor.black

Upvotes: 0

Views: 2378

Answers (2)

matt
matt

Reputation: 535889

The first view controller has this code:

let audioPlayer2 = Bundle.main.path (forResource: "Ding-Sound-Effect_qZC5gtOw3DU", ofType: "mp3")
try correctPlayer = AVAudioPlayer(contentsOf: NSURL(fileURLWithPath: audioPlayer2!)as URL)

But the second view controller does not have that code. So its correctPlayer is broken.

Upvotes: 2

DonMag
DonMag

Reputation: 77672

You are creating a correctPlayer object in View Controller 1 and you initialize it with:

try correctPlayer = AVAudioPlayer(contentsOf: NSURL(fileURLWithPath: audioPlayer2!)as URL)

That's fine.... But then in View Controller 2, you have:

var correctPlayer: AVAudioPlayer = AVAudioPlayer()

which creates a new instance of correctPlayer that is in scope only in View Controller 2, and then you're calling:

correctPlayer.play()

without having initialized it.

Inside View Controller 2 you need to again call:

    do {
        let audioPlayer2 = Bundle.main.path (forResource: "Ding-Sound-Effect_qZC5gtOw3DU", ofType: "mp3")

        try correctPlayer = AVAudioPlayer(contentsOf: NSURL(fileURLWithPath: audioPlayer2!)as URL)
    }

    catch {
        //ERROR!
    }

after which you should be able to play that sound.

Upvotes: 2

Related Questions