@IBAction func cowButton(_ sender: UIButton) {
let soundFileSeleced : String = soundArray[0]
print(soundFileSeleced)
playSound()
}
func playSound() {
let soundURL = Bundle.main.url(forResource: "cowSound", withExtension: ".wav")
do {
audioPlayer = try AVAudioPlayer(contentsOf: soundURL!)
}
catch {
print(error)
}
audioPlayer.play()
}
我无法播放我的声音。我的代码怎么了?我收到消息:
"线程1:致命错误:在解开可选值时出乎意料地发现了零"
在let soundURL
行上检查您的语法。可以肯定的是, withExtension
不应在其中包含 .
。
尝试这个?
let soundURL = Bundle.main.url(forResource: "cowSound", withExtension: "wav")