AVAudioRecorder无法正常工作

时间:2016-10-22 01:37:29

标签: ios swift avaudiorecorder

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

  

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

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()
    }

1 个答案:

答案 0 :(得分:0)

在Swift中,您不会提供error参数,因为它throws

改变这个:

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

为:

soundRecorder = try AVAudioRecorder(url: getFileURL() as URL, settings: recordSettings)
相关问题