You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何让Android的MediaRecorder使用蓝牙麦克风录音?

解决Android MediaRecorder无法使用蓝牙麦克风录音的问题

核心问题分析

你之前的尝试存在两个关键偏差:

  • 错误筛选了输出设备(GET_DEVICES_OUTPUTS + TYPE_BLUETOOTH_A2DP),蓝牙麦克风属于输入设备,需从输入设备列表中筛选
  • 未针对蓝牙麦克风配置合适的音频源与设备路由策略

可行解决方案

方案1:API 29(Android 10)及以上推荐方案

Android 10引入MediaRecorder.setAudioDevice()方法,可直接指定录音输入设备,这是最可靠的实现方式。

步骤:

  1. 获取已连接的蓝牙输入设备
  2. 在MediaRecorder.prepare()前调用setAudioDevice()绑定蓝牙麦克风

代码示例:

val audioManager = getSystemService(AUDIO_SERVICE) as AudioManager
val bluetoothInputDevices = audioManager.getDevices(AudioManager.GET_DEVICES_INPUTS)
    .filter { 
        it.type == AudioDeviceInfo.TYPE_BLUETOOTH_SCO || 
        it.type == AudioDeviceInfo.TYPE_BLUETOOTH_A2DP_SOURCE 
    }

if (bluetoothInputDevices.isNotEmpty()) {
    val bluetoothMic = bluetoothInputDevices.first()
    mediaRecorder = MediaRecorder(this).apply {
        setAudioSource(MediaRecorder.AudioSource.MIC)
        setOutputFormat(MediaRecorder.OutputFormat.MPEG_4)
        setAudioEncoder(MediaRecorder.AudioEncoder.AAC)
        setAudioChannels(1)
        setAudioSamplingRate(16000)
        setOutputFile(audioOutputFile)
        
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            setAudioDevice(bluetoothMic)
        }
        
        prepare()
        start()
    }
}

方案2:API 21(Android 5.0)到API 28兼容方案

低版本系统需通过AudioManager设置优先输入设备,配合通信模式与适配的音频源实现路由。

代码示例:

val audioManager = getSystemService(AUDIO_SERVICE) as AudioManager
val bluetoothInputDevices = audioManager.getDevices(AudioManager.GET_DEVICES_INPUTS)
    .filter { 
        it.type == AudioDeviceInfo.TYPE_BLUETOOTH_SCO || 
        it.type == AudioDeviceInfo.TYPE_BLUETOOTH_A2DP_SOURCE 
    }

if (bluetoothInputDevices.isNotEmpty()) {
    val bluetoothMic = bluetoothInputDevices.first()
    
    // 设置通信模式,确保系统优先路由蓝牙音频
    audioManager.mode = AudioManager.MODE_IN_COMMUNICATION
    
    // 指定优先使用蓝牙麦克风
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        audioManager.setPreferredDevice(bluetoothMic)
    } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        audioManager.setCommunicationDevice(bluetoothMic)
    }

    mediaRecorder = MediaRecorder(this).apply {
        // 使用VOICE_COMMUNICATION源,更适配蓝牙通话设备
        setAudioSource(MediaRecorder.AudioSource.VOICE_COMMUNICATION)
        setOutputFormat(MediaRecorder.OutputFormat.MPEG_4)
        setAudioEncoder(MediaRecorder.AudioEncoder.AAC)
        setAudioChannels(1)
        setAudioSamplingRate(16000)
        setOutputFile(audioOutputFile)
        
        prepare()
        start()
    }
}

必要权限

确保在Manifest中声明并在运行时申请以下权限:

  • android.permission.RECORD_AUDIO:基础录音权限
  • android.permission.BLUETOOTH_CONNECT:Android 12及以上蓝牙设备连接权限

内容的提问来源于stack exchange,提问作者RatherBeSailing

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 23:30:08