【问题标题】:How to control Opus bitrate in AVAudioRecorder如何在 AVAudioRecorder 中控制 Opus 比特率
【发布时间】:2019-09-07 10:07:11
【问题描述】:

我正在尝试在 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


    【解决方案1】:
    @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)
        }
    
    }
    

    【讨论】:

      【解决方案2】:

      事实证明,在这种情况下初始化 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)
      

      【讨论】:

        猜你喜欢
        • 2020-06-10
        • 2016-12-05
        • 1970-01-01
        • 2017-09-12
        • 2016-07-31
        • 2019-11-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多