自 2025 年 3 月 27 日起,我们建议您使用 android-latest-release
而非 aosp-main
构建 AOSP 并为其做出贡献。如需了解详情,请参阅 AOSP 的变更。
麦克风输入
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
在捕获音频时,音频 HAL 会收到 openInputStream
调用,其中包含指示应如何处理麦克风输入的 AudioSource
参数。
VOICE_RECOGNITION
源需要一个符合以下条件的立体声麦克风流:具有回声消除效果(如果有),但不应用任何其他处理。
若要从具有两个以上声道(立体声)的设备捕获音频,请使用声道索引掩码,而不是定位索引掩码(例如 CHANNEL_IN_LEFT
)。例如:
final AudioFormat audioFormat = new AudioFormat.Builder()
.setEncoding(AudioFormat.ENCODING_PCM_16BIT)
.setSampleRate(44100)
.setChannelIndexMask(0xf /* 4 channels, 0..3 */)
.build();
final AudioRecord audioRecord = new AudioRecord.Builder()
.setAudioFormat(audioFormat)
.build();
audioRecord.setPreferredDevice(someAudioDeviceInfo);
如果 setChannelMask
和 setChannelIndexMask
均已设置,则 AudioRecord
仅使用由 setChannelMask
设置的值(最多两个声道)。
并发捕获
从 Android 10 开始,Android 框架支持并发捕获输入,但具有保护用户隐私的限制。作为这些限制的一部分,AUDIO_SOURCE_FM_TUNER
等虚拟来源会被忽略,因此可以与常规输入(例如麦克风)同时捕获。HwAudioSource
不属于并发捕获限制的一部分。
如果应用设计用于与 AUDIO_DEVICE_IN_BUS
设备或辅助 AUDIO_DEVICE_IN_FM_TUNER
设备一起使用,则必须能够明确识别这些设备,并使用 AudioRecord.setPreferredDevice()
绕过 Android 默认声源选择逻辑。
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-03-26。
[null,null,["最后更新时间 (UTC):2025-03-26。"],[],[],null,["# Microphone input\n\nWhen capturing audio, the Audio HAL receives an `openInputStream` call that\nincludes an `AudioSource` argument to indicate how microphone input should be\nprocessed.\n\nThe `VOICE_RECOGNITION` source expects a stereo microphone stream that has an\necho cancellation effect (if available) but no other processing applied to it.\n\nMulti-channel microphone input\n------------------------------\n\nTo capture audio from a device with more than two channels (stereo), use a\nchannel index mask instead of positional index mask (such as `CHANNEL_IN_LEFT`).\nFor example: \n\n final AudioFormat audioFormat = new AudioFormat.Builder()\n .setEncoding(AudioFormat.ENCODING_PCM_16BIT)\n .setSampleRate(44100)\n .setChannelIndexMask(0xf /* 4 channels, 0..3 */)\n .build();\n final AudioRecord audioRecord = new AudioRecord.Builder()\n .setAudioFormat(audioFormat)\n .build();\n audioRecord.setPreferredDevice(someAudioDeviceInfo);\n\nWhen both `setChannelMask` and `setChannelIndexMask` are set, `AudioRecord` uses\nonly the value set by `setChannelMask` (maximum of two channels).\n\nConcurrent capture\n------------------\n\nAs of Android 10, the Android framework supports\n[Concurrent capture](/docs/core/audio/concurrent) of inputs, but with\nrestrictions to protect the user's privacy. As part of these restrictions,\nvirtual sources such as `AUDIO_SOURCE_FM_TUNER` are ignored, and are allowed to\nbe captured concurrently along with a regular input (such as the microphone).\n`HwAudioSource` is not considered part of the concurrent capture restrictions.\n\nApps designed to work with `AUDIO_DEVICE_IN_BUS` devices or with secondary\n`AUDIO_DEVICE_IN_FM_TUNER` devices must rely on explicitly identifying those\ndevices and using `AudioRecord.setPreferredDevice()` to bypass the Android\ndefault source selection logic."]]