无法获取AVAudioRecorder使用URL的代码。有人能够使Record Audio在iOS和Swift 3上运行吗?下面的粗体字是出现以下错误的地方:


  “无法使用类型为'(URL:URL,settings ...)的参数列表来为类型'AVAudioRecorder'调用初始化程序。


override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    //Dispose of any resources that can be recreated.
}

func setupRecorder(){
    let recordSettings = [AVFormatIDKey : kAudioFormatAppleLossless,
                          AVEncoderAudioQualityKey : AVAudioQuality.max.rawValue,
                          AVEncoderBitRateKey : 320000,
                          AVNumberOfChannelsKey : 2,
                          AVSampleRateKey : 44100.0 ] as [String : Any]

    var error : NSError?

    func getCacheDirectory() -> String {
        let paths = NSSearchPathForDirectoriesInDomains(.cachesDirectory,.userDomainMask, true)

        return paths [0]
    }

    func getFileURL() -> URL {
        let path = (getCacheDirectory() as NSString).strings(byAppendingPaths: ["fileName"])
        let filePath = URL(fileURLWithPath: "path")
        return filePath
    }

    // Error on the following line
    soundRecorder = try AVAudioRecorder(url: getFileURL() as URL, settings: recordSettings, error: &error)


    if let err = error {
        NSLog("There was an error")
    }
    else {
        soundRecorder.delegate = self
        soundRecorder.prepareToRecord()
    }

最佳答案

在Swift中,您无需提供error参数,因为它是throws

更改此:

soundRecorder = try AVAudioRecorder(url: getFileURL() as URL, settings: recordSettings, error: &error)


至:

soundRecorder = try AVAudioRecorder(url: getFileURL() as URL, settings: recordSettings)

关于ios - AVAudioRecorder无法正常工作,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40187486/

10-11 04:28