无法在iOS中播放声音文件


@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")

最新更新