简体   繁体   English

使用AVAudioRecorder iOS录制作品

[英]Record Opus with AVAudioRecorder iOS

I'm trying to record Opus format in iOS. 我正在尝试在iOS中记录Opus格式。 When I test the recorder with LinearPCM formate ie wav , AVAudioRecorderDelegate functions and updateMeters is being called correctly and recorder url have valid file saved at given path. 当我使用LinearPCM AVAudioRecorderDelegatewav测试记录器时,将正确调用AVAudioRecorderDelegate函数和updateMeters,并且记录器url在给定路径中保存了有效文件。 Here is the configuration I'm using for recoding. 这是我用于重新编码的配置。

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]

Then I tried to test it for Opus with following configurations but delegate functions are not being called, meters value is constant -160db(silence) and audioRecorder url is nil because I can't get it in delegate function: 然后,我尝试使用以下配置在Opus上对其进行测试,但未调用委托函数,米的值恒定为-160db(silence),而audioRecorder url为nil因为我无法在委托函数中获取它:

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

The Opus configurations I'm using: 我正在使用的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]

How can I correctly set the configurations to record Opus format for my audio social networking app. 如何正确设置配置以记录我的音频社交网络应用程序的Opus格式。 What would be best for 30 sec audio posts uploading and listening on other users's side. 最适合30秒的音频帖子在其他用户那边上传和收听。

The following configurations for recoding Opus format worked. 用于重新编码Opus格式的以下配置有效。 it doesn't work on 44100 Hz. 在44100 Hz上不起作用。 Only works for 16000 or 24000 Hz. 仅适用于16000或24000 Hz。

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

Nothing else is changes and file is being saved correctly and didFinishRecoding is also being called now. 没什么变化,文件正确保存,并且didFinishRecoding现在也被调用。

Try this: 尝试这个:

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

You need to call audioRecorder?.stop() when you want to stop recording. 要停止录制时,需要调用audioRecorder?.stop() It will give you callback in audioRecorderDidFinishRecording(_ recorder: method. Hope it helps. 它将为您提供audioRecorderDidFinishRecording(_ recorder:方法中的回调。希望对您audioRecorderDidFinishRecording(_ recorder:帮助。

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

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