簡體   English   中英

使用AVAudioRecorder iOS錄制作品

[英]Record Opus with AVAudioRecorder iOS

我正在嘗試在iOS中記錄Opus格式。 當我使用LinearPCM AVAudioRecorderDelegatewav測試記錄器時,將正確調用AVAudioRecorderDelegate函數和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秒的音頻帖子在其他用戶那邊上傳和收聽。

用於重新編碼Opus格式的以下配置有效。 在44100 Hz上不起作用。 僅適用於16000或24000 Hz。

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

沒什么變化,文件正確保存,並且didFinishRecoding現在也被調用。

嘗試這個:

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:方法中的回調。希望對您audioRecorderDidFinishRecording(_ recorder:幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM