我用蓝牙音箱实现录音机。蓝牙扬声器有一些媒体按钮,如播放/暂停,下一个,上一个。我触发播放按钮进行录制,然后再按一次停止。但问题是当使用mAudioManager.startBluetoothSco()使用蓝牙麦克风录制时,Media_button接收器不再工作,无法接收任何事件。
以下是源代码: 注册接收者:
((AudioManager) getSystemService(AUDIO_SERVICE)).registerMediaButtonEventReceiver(
new ComponentName(
this,
MediaButtonReceiver.class.getName()));
MediaButtonReceiver:
@Override
public void onReceive(Context context, Intent intent) {
Log.d("MediaButtonReceiver", "onReceive Media button!" );
KeyEvent key = (KeyEvent) intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
Log.d("MediaButtonReceiver", "Media button! action: [" + key.getAction() + "] key code: [" + key.getKeyCode() + "]");
if(key.getAction() == KeyEvent.ACTION_UP) {
int action = fromKeyCodeToAction(key.getKeyCode());
switch (action) {
case ACTION_START_STOP_TALKING:
LogUtils.LOGD("MediaButtonReceiver", "Action start/stop");
startStopRecord();
break;
...我在听取检查当前状态记录时处理了一些逻辑。并触发服务以启动或停止记录。这是开始记录的代码。
if (isBluetoothHeadsetConnected) {
LogUtils.LOGI("Bluetooth", "record using headset");
//we just start recording when the audio state is connected
mContext.registerReceiver(mBluetoothAudioStateBroadcastReceiver, mBluetoothAudioStateIntentFilter);
LogUtils.LOGI("Bluetooth", "startBluetoothSco");
mAudioManager.startBluetoothSco();
mAudioManager.setBluetoothScoOn(true);
} else {
LogUtils.LOGI("Bluetooth", "record normally ");
mAudioManager.setMode(AudioManager.MODE_NORMAL);
mAudioManager.setBluetoothScoOn(false);
handleStartRecordJob();
}
如果我删除startBluetoothSco(),接收器工作正常,但应用程序使用设备麦克风而不是蓝牙扬声器。如果我不移除,接收器不再工作,我无法停止录音机。 你有什么解决办法吗?