简体   繁体   中英

Record Opus with AVAudioRecorder iOS

I'm trying to record Opus format in iOS. 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. 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:

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

The Opus configurations I'm using:

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. What would be best for 30 sec audio posts uploading and listening on other users's side.

The following configurations for recoding Opus format worked. it doesn't work on 44100 Hz. Only works for 16000 or 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.

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. It will give you callback in audioRecorderDidFinishRecording(_ recorder: method. Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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