Swift:Avaudioplayer不播放,线程可能出错



我目前正在使用基于图像/对象的状态播放音频剪辑的应用程序。

目前我正在获取此错误代码。

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

我猜想,当已经在同一线程上播放另一个音频剪辑时尝试在同一线程上播放另一个音频剪辑可能是一个错误。

但老实说,我不确定,我仍在学习线程和多线程,所以我可能会到达这里。

我在这里想念什么?在不收到此错误消息的情况下,我该如何播放第二个音频剪辑(turnaberoffaudio(?谢谢!

这是我点击按钮时会发生的情况。

@objc func lightsaberTapped() {
    print("lightsaber open!")
    if lightsaberModel.isSaberOn == false {
        UIView.animate(withDuration: 0.2, animations: {
            self.saberImageHeightConstraint.constant = 530
            self.saberImageWidthConstraint.constant = 210
            self.mainView.layoutIfNeeded()
            self.viewDidLayoutSubviews()
        }, completion: nil)
        lightsaberModel.turnSaberOnAudio.play()
        lightsaberModel.isSaberOn = true
    } else {
        UIView.animate(withDuration: 0.2, animations: {
            self.saberImageHeightConstraint.constant = 1
            self.saberImageWidthConstraint.constant = 1
            self.mainView.layoutIfNeeded()
            self.viewDidLayoutSubviews()
        }, completion: nil)
        lightsaberModel.turnSaberOffAudio.play()
        lightsaberModel.isSaberOn = false
    }
}

这是具有必要信息的模型。

class Model {
    var turnSaberOnAudio: AVAudioPlayer = AVAudioPlayer()
    var turnSaberOffAudio: AVAudioPlayer = AVAudioPlayer()
    var whichLightsaber = String()
    var isSaberOn = Bool()
    func turnSaberOn() {
        let turnSaberOnPath = Bundle.main.path(forResource: "SaberOn", ofType: "wav")
        do {
            turnSaberOnAudio = try AVAudioPlayer(contentsOf: URL(fileURLWithPath: turnSaberOnPath!))
        }
        catch {
            print(error)
        }
    }
    func turnSaberOff() {
        let turnSaberOffPath = Bundle.main.path(forResource: "SaberOff", ofType: "mp3")
        do {
            turnSaberOffAudio = try AVAudioPlayer(contentsOf: URL(fileURLWithPath: turnSaberOffPath!))
        }
        catch {
            print(error)
        }
    }
}

这是您永远不应该做的事情:

var turnSaberOnAudio: AVAudioPlayer = AVAudioPlayer()

空的初始化器 AVAudioPlayer()创造了一些无用的东西,这不仅是无用的,而且会导致崩溃有害。

并将var宣布为非偏见,您无法区分它是否具有有用的东西。


使用您当前的代码,turnSaberOn()turnSaberOff()可能不会在您调用play()时被调用。

尝试更改Model为:

class Model {
    var turnSaberOnAudio: AVAudioPlayer? //<- Never use `AVAudioPlayer()`
    var turnSaberOffAudio: AVAudioPlayer? //<- Never use `AVAudioPlayer()`
    var whichLightsaber: String = ""
    var isSaberOn: Bool = false
    init() {
        let turnSaberOnUrl = Bundle.main.url(forResource: "SaberOn", withExtension: "wav")!
        do {
            turnSaberOnAudio = try AVAudioPlayer(contentsOf: turnSaberOnUrl)
            turnSaberOnAudio!.prepareToPlay()
            print("(turnSaberOnUrl) loaded")
        } catch {
            print(error)
        }
        let turnSaberOffUrl = Bundle.main.url(forResource: "SaberOff", withExtension: "mp3")!
        do {
            turnSaberOffAudio = try AVAudioPlayer(contentsOf: turnSaberOffUrl)
            turnSaberOffAudio!.prepareToPlay()
            print("(turnSaberOffUrl) loaded")
        } catch {
            print(error)
        }
        //...
    }
    //...
}

并这样使用:

    @IBAction func lightSaberTapped(_ sender: Any) {
        print("lightsaber open!")
        if !lightsaberModel.isSaberOn {
            //...
            lightsaberModel.turnSaberOnAudio?.play()
            lightsaberModel.isSaberOn = true
        } else {
            //...
            lightsaberModel.turnSaberOffAudio?.play()
            lightsaberModel.isSaberOn = false
        }
    }

最新更新