iOS中使用AVAudioRecorder录制Opus格式音频的配置问题
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:
- Unsupported Sample Rate: Opus does NOT support 44100Hz. It only works with these sample rates: 8000, 12000, 16000, 24000, or 48000Hz.
- Invalid Bitrate: Setting
AVEncoderBitRateKeyto 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.plistwithNSMicrophoneUsageDescription. - Playback: iOS 11+ supports Opus playback natively via
AVPlayerorAVAudioPlayer(no need for third-party libraries). - Alternative: If you need more control, consider using
AVAssetWriterinstead ofAVAudioRecorder—it’s more flexible for encoding Opus and other formats, but requires more setup.
内容的提问来源于stack exchange,提问作者Zahid Usman Cheema
相关产品推荐
相关产品推荐

