线程1:尝试播放音频时EXC_BAD_ACCESS(代码= 1,地址= 0x48)错误

时间:2019-05-29 04:24:34

标签: ios swift avaudioplayer

我试图在Xcode中按下按钮时播放音频文件。我已经将其用于以前的项目(在Xcode的早期版本中),但是现在当我尝试执行此操作时,我得到了错误。我的文件保存在主捆绑包中。

  

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

func playExpression(_ word: String){

    //sets up the audio player for the word
    var player:AVAudioPlayer = AVAudioPlayer()

    do {
        //gets the audio file and links it to the player.
        let path = Bundle.main.path(forResource: word, ofType: "mp3")
        try player = AVAudioPlayer(contentsOf: NSURL(fileURLWithPath: path!) as URL)
    }
    catch {
        /*ERROR*/
    }

    //plays the  audio clip
    player.play()
    print("audio played")
    sleep(UInt32(1)) //gives the audio clip time to play
}

@IBAction func speakerPressed(_ sender: UIButton) {
        playExpression("testaudio")
    }

0 个答案:

没有答案