线程1:EXC_BAD_ACCESS(代码= 1,地址= 0x48)

时间:2019-03-12 14:19:21

标签: ios swift xcode

我在XCODE上使用swift。我遇到了这个问题,如果我想单击我的按钮来播放声音。它会崩溃。我有viewcontrollers。第一个ViewController连接到一个Storyboard。该故事板具有按钮播放声音。当我运行它时,声音会播放。当转到第二个故事板播放声音时,它会崩溃

[2张图片是我用来播放音频的代码。我在第一个viewcontroller上粘贴了相同的代码。]这就是第二个viewcontroller上的代码。当我按下第二个情节提要板上的按钮时,我得到了线程1:EXC_BAD_ACCESS(代码= 1,地址= 0x48),但在我按下的第一个按钮上却没有得到此错误。

当我运行模拟器并进入第二个视图控制器时会发生

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-不起作用(给出错误)

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

2 个答案:

答案 0 :(得分:0)

您正在View Controller 1中创建一个correctPlayer对象,并使用以下方法对其进行了初始化:

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

那很好。...但是在View Controller 2中,您可以:

var correctPlayer: AVAudioPlayer = AVAudioPlayer()

创建correctPlayer new 实例,该实例仅在View Controller 2的作用域内,然后您调用:

correctPlayer.play()

没有初始化它。

在View Controller 2内部,您需要再次调用:

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

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

    catch {
        //ERROR!
    }

之后,您应该可以播放该声音。

答案 1 :(得分:0)

第一个视图控制器具有以下代码:

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

但是第二个视图控制器没有该代码。因此其correctPlayer已损坏。