[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間: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)."]]