【问题标题】:Record Opus with AVAudioRecorder iOS使用 AVAudioRecorder iOS 录制作品
【发布时间】:2018-10-31 18:05:43
【问题描述】:

我正在尝试在 iOS 中录制 Opus 格式。当我使用 LinearPCM 格式测试记录器时,即wavAVAudioRecorderDelegate 函数和 updateMeters 被正确调用并且记录器 url 在给定路径保存了有效文件。这是我用于重新编码的配置。

let fileMgr = FileManager.default
let dirPaths = fileMgr.urls(for: .documentDirectory,
                                in: .userDomainMask)
let soundFileURL = dirPaths[0].appendingPathComponent("audio.wav")
let recordSettings =
        [AVEncoderAudioQualityKey: AVAudioQuality.low.rawValue,
         AVEncoderBitRateKey: 16,
         AVNumberOfChannelsKey: 1,
         AVFormatIDKey : kAudioFormatLinearPCM,
         AVSampleRateKey: 44100.0] as [String : Any]

然后我尝试使用以下配置为 Opus 测试它,但没有调用委托函数,米值是恒定的 -160db(silence) 和 audioRecorder url 是 nil 因为我无法在委托函数中获取它:

func audioRecorderDidFinishRecording(_ recorder: AVAudioRecorder, successfully flag: Bool) {
    print(audioRecorder!.currentTime)
    self.audioFileUrl = audioRecorder!.url
}

我正在使用的 Opus 配置:

let soundFileURL = dirPaths[0].appendingPathComponent("audio.opus")
    let recordSettings =
        [AVEncoderAudioQualityKey: AVAudioQuality.high.rawValue,
         AVEncoderBitRateKey: 16,
         AVNumberOfChannelsKey: 1,
         AVFormatIDKey : kAudioFormatOpus,
         AVSampleRateKey: 44100.0] as [String : Any]

如何正确设置配置以录制我的音频社交网络应用程序的 Opus 格式。对于在其他用户端上传和收听的 30 秒音频帖子来说,什么是最好的。

【问题讨论】:

    标签: ios swift audio-recording opus


    【解决方案1】:

    试试这个:

    var recordSettings = Dictionary() as [String: Any]
    var audioRecorder: AVAudioRecorder?
    
    func recordAudio() {
        let fileMgr = FileManager.default
        let dirPaths = fileMgr.urls(for: .documentDirectory,
                                    in: .userDomainMask)
        let soundFileURL = dirPaths[0].appendingPathComponent("audio.wav")
        recordSettings =
            [AVEncoderAudioQualityKey: AVAudioQuality.low.rawValue,
             AVEncoderBitRateKey: 16,
             AVNumberOfChannelsKey: 1,
             AVFormatIDKey : kAudioFormatLinearPCM,
             AVSampleRateKey: 44100.0] as [String : Any]
    
        do {
            audioRecorder = try AVAudioRecorder(url: soundFileURL, settings: recordSettings)
        } catch {
            print(error.localizedDescription)
        }
    
        audioRecorder?.delegate = self
        audioRecorder?.prepareToRecord()
    
        // call record method when you want to start recording
        audioRecorder?.record()
    }
    
    func audioRecorderDidFinishRecording(_ recorder: AVAudioRecorder, successfully flag: Bool) {
    
        print(recorder.currentTime)
        do {
            audioPlayer = try AVAudioPlayer(contentsOf: recorder.url)
        } catch {
            print(error)
        }
        audioPlayer?.play()
    }
    

    当您想停止录制时,您需要致电audioRecorder?.stop()。它会在audioRecorderDidFinishRecording(_ recorder: 方法中给你回调。希望对您有所帮助。

    【讨论】:

    • 谢谢。我也在调用 stop() 函数,而我的录音机设置为 30 秒的持续时间,它会自动调用 stop() 函数。问题是它只在 Wav 格式上调用 didFinish。
    • 你想做什么?
    • 我只想录制 Opus 格式的音频 30 秒。它现在完成了。谢谢。
    • @ZahidUsmanCheema 你如何从本地文件播放作品文件?有什么想法吗?
    • @ZahidUsmanCheema 它正在播放录制的音频。但我有 .opus 文件并想将其转换为 wav 或 mp3
    【解决方案2】:

    以下用于重新编码 Opus 格式的配置有效。它不适用于 44100 Hz。仅适用于 16000 或 24000 Hz。

    let recordSettings =
            [AVNumberOfChannelsKey: 1,
             AVFormatIDKey : kAudioFormatOpus,
             AVSampleRateKey: 24000.0] as [String : Any]
    

    没有其他变化,文件正在正确保存,并且现在也调用了 didFinishRecoding。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-21
      • 2012-05-06
      • 2012-06-26
      相关资源
      最近更新 更多