如何在AVAudioRecorder中控制Opus比特率

问题描述 投票:0回答:2

我正在尝试在AVAudioRecorder中为Opus(kAudioFormatOpus)设置比特率,但它不起作用。比特率总是在20kb / s左右。

let recordSettings =
    [AVEncoderBitRateKey: 32000,
     AVFormatIDKey: kAudioFormatOpus,
     AVSampleRateKey: 16000.0] as [String: Any]
let recordingFormat = AVAudioFormat(settings: recordSettings)!
let recorder = try! AVAudioRecorder(url: url, format: recordingFormat)
recorder.record()

有什么我做错了吗?

ios swift avfoundation avaudiorecorder opus
2个回答
0
投票

原来,在这种情况下初始化AVAudioRecorder的正确方法应该是:

let recordSettings =
    [AVEncoderBitRateKey: 32000,
     AVFormatIDKey: kAudioFormatOpus,
     AVSampleRateKey: 16000.0] as [String: Any]
let recorder = try! AVAudioRecorder(url: url, settings: recordSettings)
recorder.record()

不要先尝试初始化AVAudioFormat然后将其传递给AVAudioRecorder。而是直接使用设置调用init:

AVAudioRecorder(url: url, settings: recordSettings)

0
投票
@IBOutlet var recordButton: UIButton!
var recorder: AVAudioRecorder!
var player: AVAudioPlayer!

@IBAction func recordBtnACtn(_ sender: Any){
    if player != nil && player.isPlaying {
        print("stopping")
        player.stop()
    }
    if recorder == nil {
        print("recording. recorder nil")
        recordButton.setTitle("Pause", for: .normal)
        recordWithPermission(true)
        return
    }
    if recorder != nil && recorder.isRecording {
        print("pausing")
        recorder.pause()
        recordButton.setTitle("Continue", for: .normal)
    } else {
        print("recording")
        recordButton.setTitle("Pause", for: .normal)

        recordWithPermission(false)
    }
}


func recordWithPermission(_ setup: Bool) {
    print("\(#function)")

    AVAudioSession.sharedInstance().requestRecordPermission {
        [unowned self] granted in
        if granted {
            DispatchQueue.main.async {
                print("Permission to record granted")
                self.setSessionPlayAndRecord()
                if setup {
                    self.setupRecorder()
                }
                self.recorder.record()
                self.meterTimer = Timer.scheduledTimer(timeInterval: 0.1,
                                                       target: self,
                                                       selector: #selector(self.updateAudioMeter(_:)),
                                                       userInfo: nil,
                                                       repeats: true)
            }
        }else{
            print("Permission to record not granted")
        }
    }
    if AVAudioSession.sharedInstance().recordPermission() == .denied {
        print("permission denied")
    }
}



func setupRecorder() {
    print("\(#function)")

    let format = DateFormatter()
    format.dateFormat="yyyy-MM-dd-HH-mm-ss"
    let currentFileName = "recording-\(format.string(from: Date())).m4a"
    print(currentFileName)

    let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
    self.soundFileURL = documentsDirectory.appendingPathComponent(currentFileName)
    print("writing to soundfile url: '\(soundFileURL!)'")

    if FileManager.default.fileExists(atPath: soundFileURL.absoluteString) {
        // probably won't happen. want to do something about it?
        print("soundfile \(soundFileURL.absoluteString) exists")
    }

    let recordSettings: [String: Any] = [
        AVFormatIDKey: kAudioFormatAppleLossless,
        AVEncoderAudioQualityKey: AVAudioQuality.max.rawValue,
        AVEncoderBitRateKey: 32000,
        AVNumberOfChannelsKey: 2,
        AVSampleRateKey: 44100.0
    ]
    do {
        recorder = try AVAudioRecorder(url: soundFileURL, settings: recordSettings)
        recorder.delegate = self
        recorder.isMeteringEnabled = true
        recorder.prepareToRecord() // creates/overwrites the file at soundFileURL
    } catch {
        recorder = nil
        print(error.localizedDescription)
    }

}
© www.soinside.com 2019 - 2024. All rights reserved.