无法修复我的播放音频函数 - 错误参数标签'(contentsOf:, error:)'与任何可用的重载都不匹配



我不能由于错误:参数标签'(contentsofurl::::::)'不匹配任何可用的过载

,因此无法编写AudioPlayer零件。

我已经试图将其写成contentof而不是URL或更改一些括号,但这里没有幸运。有人可以帮忙吗?

功能下面:

@IBAction func playAudio(sender: AnyObject) {
        if audioRecorder?.isRecording == false{
            stopButton.isEnabled = true
            recordButton.isEnabled = false
            let error : NSError?
            do {
                audioPlayer = try AVAudioPlayer(contentsOfURL: audioRecorder!.url, error: error)
            audioPlayer?.delegate = self as? AVAudioPlayerDelegate
            if let err = error{
                print("audioPlayer error: (err.localizedDescription)")
            }else{
                audioPlayer?.play()
            }
        }
    }

正确的avaudioplayer类init是:

init(contentsOf: URL)

检查如何实现Do Catch Block,您的代码变为:

do {
   audioPlayer = try AVAudioPlayer(contentsOf: YourURL)
   audioPlayer?.delegate = self
   audioPlayer?.play()
} catch let error {
   print("audioPlayer error: (error.localizedDescription)")
}

最新更新