iOS录制时能否选择底部左右内置麦克风?AVAudioSession API疑问
Great question! Let’s break this down clearly for you:
Short Answer
You cannot directly use AVAudioSessionOrientationLeft or AVAudioSessionOrientationRight to select a single bottom microphone on iPhone 6s and later models with dual bottom mics. Those orientation enums serve a completely different purpose than individual mic selection.
Why Those Enums Don’t Work for This
- The
AVAudioSessionOrientation*values are tied to the physical orientation of the device (e.g., when you rotate your iPhone to landscape left/right), not to picking specific microphones. Their job is to align audio input/output with the device’s current orientation, not to target individual mics. - Apple treats the two bottom mics as a single "Bottom" audio source group. There’s no official API to target just the left or right bottom mic directly via session orientation settings.
How to Record with a Single Bottom Mic (Workaround)
If you need to capture audio from only one of the two bottom mics, you’ll need to work with the stereo audio input they produce and isolate a single channel. Here’s how you can approach it:
- Identify the bottom microphone device
UseAVCaptureDeviceto find the built-in bottom microphone (you can filter by name or position, though position may be.unspecifiedfor bottom mics). - Access the stereo audio channels
The bottom mics output a 2-channel stereo signal, where each channel corresponds to one of the two mics. - Isolate the desired channel
During recording (viaAVCaptureAudioDataOutputorAVAudioEngine), process the audio samples to keep only the left or right channel.
Here’s a quick Swift code snippet to get you started:
// Discover all built-in microphones let micDiscoverySession = AVCaptureDevice.DiscoverySession( deviceTypes: [.builtInMicrophone], mediaType: .audio, position: .unspecified ) // Locate the bottom microphone (adjust the filter based on your device's mic naming) if let bottomMic = micDiscoverySession.devices.first(where: { $0.localizedName.lowercased().contains("bottom") }) { do { try bottomMic.lockForConfiguration() print("Bottom mic supports \(bottomMic.activeFormat.channelCount) channels") bottomMic.unlockForConfiguration() // Set up capture session to receive audio data let captureSession = AVCaptureSession() let audioInput = try AVCaptureDeviceInput(device: bottomMic) if captureSession.canAddInput(audioInput) { captureSession.addInput(audioInput) } let audioOutput = AVCaptureAudioDataOutput() audioOutput.setSampleBufferDelegate(self, queue: DispatchQueue(label: "audioQueue")) if captureSession.canAddOutput(audioOutput) { captureSession.addOutput(audioOutput) } captureSession.startRunning() } catch { print("Error configuring microphone: \(error.localizedDescription)") } } // In your AVCaptureAudioDataOutputSampleBufferDelegate implementation: func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) { // Convert sample buffer to audio data, then extract left/right channel // For example, use AudioBufferList to access individual channels }
Key Note
Even though AVAudioSessionOrientationLeft and AVAudioSessionOrientationRight haven’t been deprecated, they’re not designed for this use case. Apple’s API design prioritizes treating dual bottom mics as a stereo pair, so channel isolation is the supported way to get audio from a single one.
内容的提问来源于stack exchange,提问作者Forte Zhu

