遇到客戶一個需求,如果連接了帶mic的藍牙耳機,默認所有的錄音要走藍牙mic通道。
這個功能搞了好久,終于搞定了。
1. 向RK尋求幫助,先打通 bt sco能力。
此時,還無法默認就切換到藍牙 mic通道,接下來我們需求默認切換到藍牙通道。
2. 修改frameworks/av/services/audiopolicy/enginedefault/src/Engine.cpp,
默認走bt sco的mic錄音輸入源? ? ?
switch (inputSource) {
case AUDIO_SOURCE_DEFAULT:
case AUDIO_SOURCE_MIC:
device = availableDevices.getDevice(
AUDIO_DEVICE_IN_BLUETOOTH_A2DP, String8(""), AUDIO_FORMAT_DEFAULT);
if (device != nullptr) break;
if (audio_is_bluetooth_out_sco_device(commDeviceType)) {
device = availableDevices.getDevice(
AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET, String8(""), AUDIO_FORMAT_DEFAULT);
if (device != nullptr) break;
}
device = availableDevices.getFirstExistingDevice({
+ ???????????????AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET,
AUDIO_DEVICE_IN_BLE_HEADSET, AUDIO_DEVICE_IN_WIRED_HEADSET,
AUDIO_DEVICE_IN_USB_HEADSET, AUDIO_DEVICE_IN_USB_DEVICE,
AUDIO_DEVICE_IN_BLUETOOTH_BLE, AUDIO_DEVICE_IN_BUILTIN_MIC});
break;
case AUDIO_SOURCE_CAMCORDER:
// For a device without built-in mic, adding usb device
device = availableDevices.getFirstExistingDevice({
- ? ? ? ? ? ? ? AUDIO_DEVICE_IN_USB_DEVICE,
+ ? ? ? ? ? ? ?AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET, AUDIO_DEVICE_IN_USB_DEVICE,
AUDIO_DEVICE_IN_BACK_MIC, AUDIO_DEVICE_IN_BUILTIN_MIC});
break;
case AUDIO_SOURCE_VOICE_DOWNLINK:
3. 很關鍵的一步,在一個常駐service里邊(或者可以在system ui模塊實現)監聽插入藍牙耳機,進行默認sco模式切換
IntentFilter filter = new IntentFilter(); filter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED); filter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED); registerReceiver(myReceiver, filter);
BroadcastReceiver myReceiver = new BroadcastReceiver() {@Overridepublic void onReceive(Context context, Intent intent) {if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(intent.getAction())) {Log.d("mod@bt", "bt connected");startBluetoothRecording(context);});} else if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(intent.getAction())) {Log.d("mod@bt", "bt disconnected");stopRecording(context);}}} };
? ? public void startBluetoothRecording(Context context) {
try {
AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
? ? ? ? ? ? if (!audioManager.isBluetoothScoOn()) {
audioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
audioManager.setBluetoothScoOn(true);
audioManager.startBluetoothSco();
}
} catch (Exception e) {
e.printStackTrace();
}
}
? ? public void stopRecording(Context context) {
try {
? ? ? ? ? ? AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
? ? ? ? ? ? if (audioManager.isBluetoothScoOn()) {
audioManager.setBluetoothScoOn(false);
audioManager.stopBluetoothSco();
? ? ? ? ? ? }
? ? ? ? ? ? audioManager.setMode(AudioManager.MODE_NORMAL);
audioManager.setSpeakerphoneOn(true);
} catch (Exception e) {
e.printStackTrace();
}
}
4. 如上操作后,每次即使應用選擇了系統默認mic,也會走bt sco通道錄音。
5. 當然,可能會遇到其他的優化問題,如果你有需求,歡迎給我留言。