ios - AVAudioRecorder 不工作

标签 ios swift avaudiorecorder

无法获取 AVAudioRecorder 与 URL 配合使用的代码。有人能够在 iOS 和 Swift 3 上录制音频吗?下面的粗体文本是我收到以下错误的地方:

"Cannot invoke initializer for type 'AVAudioRecorder' with an argument list of type '(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()
    }

最佳答案

在 Swift 中,您无需提供 error 参数,因为它会抛出

更改此:

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/

相关文章:

ios - 为什么 URL 在存储然后从本地文件系统检索时会被截断?

ios - 将我的应用程序生成的所有声音记录在音频文件中(不是来自麦克风)

ios - 当粒子发射器在屏幕上时无法单击 UIButton

objective-c - 屏幕锁定时 AVAudioRecorder 不录制

iOS 7 m4a 到 mp3 编码 - 这可能吗?

ios - 什么时候使用 removeFromSuperview?

ios - 解析中的 block

ios - UICollectionView 的动态单元格宽度取决于标签宽度

iphone - OpenAL 在循环播放声音时出现故障

node.js - Firebase FCM : Different Sound Settings for Topic Push Notifications