无法在iOS中获取声音文件

时间:2017-12-09 03:05:51

标签: ios swift

@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:致命错误:在解包可选值时意外发现nil”

1 个答案:

答案 0 :(得分:0)

检查let soundURL行的语法。非常确定withExtension不应该包含.

试试这个?

let soundURL = Bundle.main.url(forResource: "cowSound", withExtension: "wav")