录音
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
基于蓝牙低功耗 (BLE) 音频的蓝牙音频配置文件
允许双向流式传输高品质音频(例如立体声音频)
(采样率为 32 kHz)。这要归功于 LE 的创建,
等时通道 (ISO)。ISO 类似于同步连接导向型
(SCO) 进行链路连接,因为它也使用预留的无线带宽,
预留不再上限为 64 Kbps,并且可以动态调整。
蓝牙音频输入可以使用最新的
AudioManager API,适合几乎所有用途
支持请求除外。本指南介绍了如何在 Android 设备上
BLE 音频耳穿戴设备。
首先,配置您的应用,使其以 build.gradle
中的正确 SDK 为目标:
targetSdkVersion 31
注册音频回调
创建
AudioDeviceCallback
通知您的应用关于已连接或断开连接的任何更改
AudioDevices
。
final AudioDeviceCallback audioDeviceCallback = new AudioDeviceCallback() {
@Override
public void onAudioDevicesAdded(AudioDeviceInfo[] addedDevices) {
};
@Override
public void onAudioDevicesRemoved(AudioDeviceInfo[] removedDevices) {
// Handle device removal
};
};
audioManager.registerAudioDeviceCallback(audioDeviceCallback);
查找 BLE 音频设备
获取所有已连接且支持输入的音频设备的列表,然后使用
getType()
,看看是否
该设备是
AudioDeviceInfo.TYPE_BLE_HEADSET
。
Kotlin
val allDeviceInfo = audioManager.getDevices(GET_DEVICES_INPUTS)
var bleInputDevice: AudioDeviceInfo? = null
for (device in allDeviceInfo) {
if (device.type == AudioDeviceInfo.TYPE_BLE_HEADSET) {
bleInputDevice = device
break
}
}
Java
AudioDeviceInfo[] allDeviceInfo = audioManager.getDevices(GET_DEVICES_INPUTS);
AudioDeviceInfo bleInputDevice = null;
for (AudioDeviceInfo device : allDeviceInfo) {
if (device.getType() == AudioDeviceInfo.TYPE_BLE_HEADSET) {
bleInputDevice = device;
break;
}
}
立体声支持
要检查所选设备是否支持立体声麦克风,请查看
设备具有两个或更多信道。如果设备只有一个通道,请将通道掩码设置为单声道。
Kotlin
var channelMask: Int = AudioFormat.CHANNEL_IN_MONO
if (audioDevice.channelCounts.size >= 2) {
channelMask = AudioFormat.CHANNEL_IN_STEREO
}
Java
if (bleInputDevice.getChannelCounts() >= 2) {
channelMask = AudioFormat.CHANNEL_IN_STEREO;
};
设置录音器
您可以使用标准的 AudioRecord
构建器来设置录音器。
你可以使用声道掩码来选择立体声或单声道配置。
Kotlin
val recorder = AudioRecord.Builder()
.setAudioSource(MediaRecorder.AudioSource.MIC)
.setAudioFormat(AudioFormat.Builder()
.setEncoding(AudioFormat.ENCODING_PCM_16BIT)
.setSampleRate(32000)
.setChannelMask(channelMask)
.build())
.setBufferSizeInBytes(2 * minBuffSizeBytes)
.build()
Java
AudioRecord recorder = new AudioRecord.Builder()
.setAudioSource(MediaRecorder.AudioSource.MIC)
.setAudioFormat(new AudioFormat.Builder()
.setEncoding(AudioFormat.ENCODING_PCM_16BIT)
.setSampleRate(32000)
.setChannelMask(channelMask)
.build())
.setBufferSizeInBytes(2*minBuffSizeBytes)
.build();
设置首选设备
设置首选设备会告知音频recorder
哪个音频设备
录制视频时使用的音频格式
Kotlin
recorder.preferredDevice = audioDevice
Java
recorder.setPreferredDevice(bleInputDevice);
现在,您可以按照 MediaRecorder 指南中的说明录制音频。
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-18。
[null,null,["最后更新时间 (UTC):2025-08-18。"],[],[],null,["# Audio recording\n\nBluetooth audio profiles based on Bluetooth Low Energy (BLE) Audio\nallow bidirectional streaming of high quality audio (for example, stereo audio\nwith a 32 kHz sampling rate). This is possible thanks to the creation of the LE\nIsochronous Channel (ISO). ISO is similar to the Synchronous Connection-Oriented\n(SCO) Link because it also uses reserved wireless bandwidth, but the bandwidth\nreservation is no longer capped at 64 Kbps and can be dynamically adjusted.\n\nBluetooth audio input can use the latest\n[AudioManager API](/reference/android/media/AudioManager) for nearly all use\ncases, excluding phone calls. This guide covers how to record stereo audio from\nBLE Audio hearables.\n\nConfigure your application\n--------------------------\n\nFirst, configure your application to target the correct SDK in `build.gradle`: \n\n targetSdkVersion 31\n\nRegister audio callback\n-----------------------\n\nCreate an\n[`AudioDeviceCallback`](/reference/android/media/AudioManager#registerAudioDeviceCallback(android.media.AudioDeviceCallback,%20android.os.Handler))\nthat lets your application know of any changes to connected or disconnected\n`AudioDevices`. \n\n final AudioDeviceCallback audioDeviceCallback = new AudioDeviceCallback() {\n @Override\n public void onAudioDevicesAdded(AudioDeviceInfo[] addedDevices) {\n };\n @Override\n public void onAudioDevicesRemoved(AudioDeviceInfo[] removedDevices) {\n // Handle device removal\n };\n };\n\n audioManager.registerAudioDeviceCallback(audioDeviceCallback);\n\nFind BLE Audio Device\n---------------------\n\nGet a list of all connected audio devices with input supported, then use\n[`getType()`](/reference/android/media/AudioDeviceInfo#getType()) to see if\nthe device is a headset using\n[`AudioDeviceInfo.TYPE_BLE_HEADSET`](/reference/android/media/AudioDeviceInfo#TYPE_BLE_HEADSET). \n\n### Kotlin\n\n```kotlin\nval allDeviceInfo = audioManager.getDevices(GET_DEVICES_INPUTS)\nvar bleInputDevice: AudioDeviceInfo? = null\n for (device in allDeviceInfo) {\n if (device.type == AudioDeviceInfo.TYPE_BLE_HEADSET) {\n bleInputDevice = device\n break\n }\n }\n```\n\n### Java\n\n```java\nAudioDeviceInfo[] allDeviceInfo = audioManager.getDevices(GET_DEVICES_INPUTS);\nAudioDeviceInfo bleInputDevice = null;\nfor (AudioDeviceInfo device : allDeviceInfo) {\n if (device.getType() == AudioDeviceInfo.TYPE_BLE_HEADSET) {\n bleInputDevice = device;\n break;\n }\n}\n```\n\nStereo support\n--------------\n\nTo check if stereo microphones are supported on the selected device, see if the\ndevice has two or more channels. If the device only has one channel, set the channel mask to mono. \n\n### Kotlin\n\n```kotlin\nvar channelMask: Int = AudioFormat.CHANNEL_IN_MONO\nif (audioDevice.channelCounts.size \u003e= 2) {\n channelMask = AudioFormat.CHANNEL_IN_STEREO\n}\n```\n\n### Java\n\n```java\nif (bleInputDevice.getChannelCounts() \u003e= 2) {\n channelMask = AudioFormat.CHANNEL_IN_STEREO;\n};\n```\n\nSet up the audio recorder\n-------------------------\n\nAudio recorders can be set up using the standard `AudioRecord` builder.\nUse the channel mask to select stereo or mono configuration. \n\n### Kotlin\n\n```kotlin\nval recorder = AudioRecord.Builder()\n .setAudioSource(MediaRecorder.AudioSource.MIC)\n .setAudioFormat(AudioFormat.Builder()\n .setEncoding(AudioFormat.ENCODING_PCM_16BIT)\n .setSampleRate(32000)\n .setChannelMask(channelMask)\n .build())\n .setBufferSizeInBytes(2 * minBuffSizeBytes)\n .build()\n```\n\n### Java\n\n```java\nAudioRecord recorder = new AudioRecord.Builder()\n .setAudioSource(MediaRecorder.AudioSource.MIC)\n .setAudioFormat(new AudioFormat.Builder()\n .setEncoding(AudioFormat.ENCODING_PCM_16BIT)\n .setSampleRate(32000)\n .setChannelMask(channelMask)\n .build())\n .setBufferSizeInBytes(2*minBuffSizeBytes)\n .build();\n```\n\nSet preferred device\n--------------------\n\nSetting a preferred device informs the audio `recorder` which audio device\nyou wish to record with. \n\n### Kotlin\n\n```kotlin\nrecorder.preferredDevice = audioDevice\n```\n\n### Java\n\n```java\nrecorder.setPreferredDevice(bleInputDevice);\n```\n| **Note:** The user can manually override this preference in device settings.\n\nNow, you can record audio as outlined in [the MediaRecorder guide](/guide/topics/media/mediarecorder)."]]