You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

iOS中使用AVAudioRecorder录制Opus格式音频的配置问题

Fixing Opus Audio Recording Issues on iOS & Optimal Configuration for Social Posts

Hey there, let's break down how to resolve your Opus recording problem and find the best setup for your 30-second social audio posts.

Why Your Current Opus Setup Isn't Working

Your issues (untriggered delegate methods, -160dB meter readings, missing file URL) stem from two critical misconfigurations:

  1. Unsupported Sample Rate: Opus does NOT support 44100Hz. It only works with these sample rates: 8000, 12000, 16000, 24000, or 48000Hz.
  2. Invalid Bitrate: Setting AVEncoderBitRateKey to 16 means 16 bits per second—way too low for any usable audio. You need to specify a value in bits per second (e.g., 32000 for 32kbps).

Corrected Opus Recording Configuration

Here's a fixed version of your setup that will work with AVAudioRecorder:

let fileMgr = FileManager.default
let dirPaths = fileMgr.urls(for: .documentDirectory, in: .userDomainMask)
let soundFileURL = dirPaths[0].appendingPathComponent("audio.opus")

// Correct Opus-compatible settings
let recordSettings: [String: Any] = [
    AVEncoderAudioQualityKey: AVAudioQuality.high.rawValue,
    AVEncoderBitRateKey: 48000, // 48kbps—balances quality and file size
    AVNumberOfChannelsKey: 1, // Mono is ideal for social audio
    AVFormatIDKey: kAudioFormatOpus,
    AVSampleRateKey: 48000.0 // Opus's recommended optimal sample rate
]

// Initialize recorder (don't forget error handling!)
do {
    audioRecorder = try AVAudioRecorder(url: soundFileURL, settings: recordSettings)
    audioRecorder.delegate = self
    audioRecorder.isMeteringEnabled = true
    audioRecorder.prepareToRecord()
    audioRecorder.record()
} catch let error {
    print("Recorder init failed: \(error.localizedDescription)")
}

Fix for Delegate Method

Also, avoid using your own audioRecorder property in the delegate callback—use the provided recorder parameter instead to prevent nil issues:

func audioRecorderDidFinishRecording(_ recorder: AVAudioRecorder, successfully flag: Bool) {
    print("Recording duration: \(recorder.currentTime)")
    if flag {
        self.audioFileUrl = recorder.url
        print("Recording saved to: \(recorder.url)")
    } else {
        print("Recording failed to finish successfully")
    }
}

Optimal Configuration for 30-Second Social Posts

For social audio (where fast uploads and good playback quality are key), use these settings:

  • Sample Rate: 48000Hz (Opus’s most efficient sample rate for full-range audio)
  • Channels: 1 (Mono cuts file size in half without noticeable quality loss for voice/social audio)
  • Bitrate: 32–64kbps
    • 32kbps: Great for voice-only content (small file size ~120KB for 30s)
    • 48–64kbps: Ideal for mixed audio (voice + background sounds, ~180–240KB for 30s)
  • Quality: AVAudioQuality.high (works well paired with the above bitrates)

Extra Notes

  • Permissions: Ensure you’ve requested microphone access in Info.plist with NSMicrophoneUsageDescription.
  • Playback: iOS 11+ supports Opus playback natively via AVPlayer or AVAudioPlayer (no need for third-party libraries).
  • Alternative: If you need more control, consider using AVAssetWriter instead of AVAudioRecorder—it’s more flexible for encoding Opus and other formats, but requires more setup.

内容的提问来源于stack exchange,提问作者Zahid Usman Cheema

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 09:19:25