我的要求是根據用戶選擇在藍牙和手機揚聲器之間切換音頻。 下面的代碼片段:Android:在藍牙和手機之間切換音頻揚聲器不一致
//AudioTrack for incoming audio to play as below:
int mMaxJitter = AudioTrack.getMinBufferSize(8000, AudioFormat.CHANNEL_OUT_MONO,AudioFormat.ENCODING_PCM_16BIT);
new AudioTrack(AudioManager.STREAM_VOICE_CALL,8000,
AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT,
mMaxJitter, AudioTrack.MODE_STREAM);
//To register broadcast receiver for bluetooth audio routing
IntentFilter ifil = new IntentFilter();
ifil.addAction(AudioManager.ACTION_SCO_AUDIO_STATE_UPDATED);
this.registerReceiver(<receiver instance>,ifil);
//To get AudioManager service
AudioManager mAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
//Whenever user select to route audio to Bluetooth
mAudioManager.setMode(AudioManager.MODE_IN_CALL);//tried setting with other mode also viz. MODE_NORMAL, MODE_IN_COMMUNICATION but no luck
mAudioManager.startBluetoothSco();//after this I get AudioManager.SCO_AUDIO_STATE_CONNECTED state in the receiver
mAudioManager.setBluetoothScoOn(true);
mAudioManager.setSpeakerphoneOn(false);
//Whenever user select to route audio to Phone Speaker
mAudioManager.setMode(AudioManager.MODE_NORMAL);
mAudioManager.stopBluetoothSco();//after this I get AudioManager.SCO_AUDIO_STATE_DISCONNECTED state in the receiver
mAudioManager.setBluetoothScoOn(false);
mAudioManager.setSpeakerphoneOn(true);
問題: 1.我能夠將音頻路由,但行爲很不一致,有時其路由到手機揚聲器,即使用戶選擇路線藍牙(藍牙連接) 2.如果音頻路由到手機揚聲器,音量變低(請不要說檢查手機音量) 3.只有幾次我可以觀察音頻路由選擇是否合適,如果我重複它,會變得很奇怪我上面提到過。
Android版本:4.3的Jellybean
有沒有人遇到一些類似的行爲?
謝謝!
我發現對於揚聲器,我不得不改變爲'MODE_NORMAL'如果它來自BT,則爲'MODE_IN_COMMUNICATION' – behelit