我试图在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")
}