简体   繁体   English

AVAudioRecorder 仅适用于.record(forDuration:)

[英]AVAudioRecorder only works with .record(forDuration:)

For the life of me I have not been able to get AVAudioRecorder to properly record sound with the record() method.在我的一生中,我一直无法让AVAudioRecorder使用record()方法正确录制声音。 I can only get sound to record if I use the record(forDuration:) method.如果我使用record(forDuration:)方法,我只能录制声音。 I've tried across multiple devices (iPhone 8 w/ iOS 14, iPad Air (7th gen) w/ iOS 14, iPhone 12 mini simulator).我尝试过多个设备(iPhone 8 w/iOS 14、iPad Air(第 7 代)w/iOS 14、iPhone 12 迷你模拟器)。 I've also changed the build target from iOS 13 to iOS 9 and tried on older simulators (iPhone 8 w/ iOS 10).我还将构建目标从 iOS 13 更改为 iOS 9 并尝试使用较旧的模拟器(iPhone 8 w/iOS 10)。

Regardless, of my device/iOS combination the behavior is always the same - when stopping the recording record() always produces an empty 4kb audio file.不管怎样,在我的设备/iOS 组合中,行为总是相同的——当停止录音时, record()总是产生一个空的 4kb 音频文件。 I've explored the simulator containers, and downloaded the device containers to verify this.我探索了模拟器容器,并下载了设备容器来验证这一点。 Again, record(forDuration:) will save a proper audio file with the actual recorded sound.同样, record(forDuration:)将使用实际录制的声音保存适当的音频文件。

Xcode Version: 12.3 Xcode 版本:12.3

Culprit code:罪魁祸首代码:

func recordAudio() {
        let dirPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as String
        let recordingName = "recordedVoice.m4a"
        let pathArray = [dirPath, recordingName]
        let filePath = URL(string: pathArray.joined(separator: "/"))
        
        let session = AVAudioSession.sharedInstance()
        try! session.setCategory(.playAndRecord, mode: .default, options: .defaultToSpeaker)
        try! session.setActive(true)
        
        try! self.audioRecorder = AVAudioRecorder(url: filePath!, settings: [:])
        self.audioRecorder.delegate = self
        self.audioRecorder.isMeteringEnabled = true
        self.audioRecorder.prepareToRecord()

        // Problematic section
        // I only use one of the following two lines, record() fails, record(forDuration:) works.
        self.audioRecorder.record()
        self.audioRecorder.record(forDuration: 5)
}

func stopRecord() {
        self.audioRecorder.stop()
        let audioSession = AVAudioSession.sharedInstance()
        try! audioSession.setActive(false)
}

Silent failure for record() - no Errors or crashes. record()的静默失败 - 没有错误或崩溃。

Another interesting thing to note that I just discovered.我刚刚发现的另一件有趣的事情。 When I call audioRecorder.stop() the app fails to save the audio file properly.当我调用audioRecorder.stop()时,应用程序无法正确保存音频文件。 Regardless of whether I use record() or record(forDuration:)不管我是使用record()还是record(forDuration:)

I'm very confused.我很困惑。 Any help is appreciated.任何帮助表示赞赏。

Silent failure for record() - no Errors or crashes. record() 的静默失败 - 没有错误或崩溃。

I get a 'fmt?'我得到一个'fmt?' error when instantiating the AVAudioRecorder .实例化AVAudioRecorder时出错。 Providing a settings dictionary fixes it.提供设置字典修复它。

let settings = [
    AVFormatIDKey: kAudioFormatMPEG4AAC,
    AVSampleRateKey: 44100,
    AVNumberOfChannelsKey: 1
]

try! self.audioRecorder = AVAudioRecorder(url: filePath!, settings: settings)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM