Android蓝牙耳机录音

Android蓝牙耳机录音
使用蓝牙耳机录音都需要开启耳机的SCO连接,这是一种双向语音通信的连接,开启蓝牙耳机的SCO连接有两种方式,一种是startBluetoothSco()的方式,另一种是startVoiceRecognition()的方式。
一、使用startBluetoothSco()的方式开启SCO通道:
蓝牙耳机录音的步骤:
1)初始化音频对象:
audiosource = MediaRecorder.AudioSource.VOICE_COMMUNICATION;
this.bufferSize = AudioRecord.getMinBufferSize(VoiceConfig.SAMPLERATE,
AudioFormat.CHANNEL_IN_MONO,
AudioFormat.ENCODING_PCM_16BIT) ;
this.audioRecord = new AudioRecord(audiosource,
VoiceConfig.SAMPLERATE,
AudioFormat.CHANNEL_IN_MONO,
AudioFormat.ENCODING_PCM_16BIT,
this.bufferSize);
mAudioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
2)启动蓝牙耳机SCO通道:
mAudioManager.startBluetoothSco();
3)读取蓝牙耳机录音数据:
audioRecord.read(buffer, 0, bufferSize);
4)关闭SCO通道:
audioManager.stopBluetoothSco();
二、使用startVoiceRecognition()的方式开启SCO通道:
步骤如下:
1)初始化:
BluetoothAdapter adapter;
if(android.os.Build.VERSION.SDK_INT adapter=BluetoothAdapter.getDefaultAdapter();
} else{
BluetoothManager bm=(BluetoothManager) BaseApplication.getContext().getSystemService(Context.BLUETOOTH_SERVICE);
adapter=bm.getAdapter();
}
BluetoothProfile.ServiceListener blueHeadsetListener = new BlueToothListener();
adapter.getProfileProxy(BaseApplication.getContext(), blueHeadsetListener, BluetoothProfile.HEADSET);
2)获取BluetoothHeadset对象和BluetoothDevice对象:
public class BlueToothListener implements BluetoothProfile.ServiceListener {
@Override
public void onServiceDisconnected(int profile) {// dont care
headset = null;
}
@Override
public void onServiceConnected(int profile, BluetoothProfile proxy) {// dont care
try {
if (proxy instanceof BluetoothHeadset)
headset = ((BluetoothHeadset) proxy);
for (BluetoothDevice device : connectedDevices) {
LogUtil.e(“BluetoothHeadset”,“BluetoothDevice found :” + device);
bluetoothDevice = device;
}
}
}
3)启动SCO连接:
headset.startVoiceRecognition(device)
4)关闭SCO连接:
headset.stopVoiceRecognition(bluetoothDevice);

**注意事项:**1)蓝牙耳机开启SCO通道后,Android系统无法监听耳机上的按键消息,因此如果想要监听蓝牙耳机上的按键消息,必须在开启SCO通道前或关闭SCO通道后监听按键消息的广播。
2)监听蓝牙耳机SCO通道开启或关闭只需要注册ACTION_AUDIO_STATE_CHANGED或ACTION_SCO_AUDIO_STATE_UPDATED广播

你可能感兴趣的:(Android蓝牙耳机录音)