2014-03-31 175 views
1

我的要求是根據用戶選擇在藍牙和手機揚聲器之間切換音頻。 下面的代碼片段: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

有沒有人遇到一些類似的行爲?

謝謝!

回答

8

我有不一致的音頻路由的原因,那是因爲我設置手機喇叭假的,我也用不恰當的方式... 下面結合工作對我來說:

//For BT 
mAudioManager.setMode(AudioManager.MODE_IN_COMMUNICATION); 
mAudioManager.startBluetoothSco(); 
mAudioManager.setBluetoothScoOn(true); 

//For phone ear piece 
mAudioManager.setMode(AudioManager.MODE_IN_COMMUNICATION); 
mAudioManager.stopBluetoothSco(); 
mAudioManager.setBluetoothScoOn(false); 
mAudioManager.setSpeakerphoneOn(false); 

//For phone speaker(loadspeaker) 
mAudioManager.setMode(AudioManager.MODE_NORMAL); 
mAudioManager.stopBluetoothSco(); 
mAudioManager.setBluetoothScoOn(false); 
mAudioManager.setSpeakerphoneOn(true); 

Android版本:4.3

謝謝!

+1

我發現對於揚聲器,我不得不改變爲'MODE_NORMAL'如果它來自BT,則爲'MODE_IN_COMMUNICATION' – behelit

0

,如果它仍然與一個人,這是我的解決方案:

(三星S7 SM-g9307測試了Android 6.0.1版本)

public class AudioSourceUtil { 

private static void reset(AudioManager audioManager) { 
    if (audioManager != null) { 
     audioManager.setMode(AudioManager.MODE_NORMAL); 
     audioManager.stopBluetoothSco(); 
     audioManager.setBluetoothScoOn(false); 
     audioManager.setSpeakerphoneOn(false); 
     audioManager.setWiredHeadsetOn(false); 
    } 
} 

public static void connectEarpiece(AudioManager audioManager) { 
    reset(audioManager); 
    audioManager.setMode(AudioManager.MODE_IN_COMMUNICATION); 
} 

public static void connectSpeaker(AudioManager audioManager) { 
    reset(audioManager); 
    audioManager.setSpeakerphoneOn(true); 
} 

public static void connectHeadphones(AudioManager audioManager) { 
    reset(audioManager); 
    audioManager.setWiredHeadsetOn(true); 
} 

public static void connectBluetooth(AudioManager audioManager) { 
    reset(audioManager); 
} 

}

而爲(標籤版式中的標籤):

/** 
* There are 3 scenarios for the audio source: 
* 1. No headphones and no bluetooth device: toggle phone/ speaker 
* 2. Headphones connected: toggle headphones/ speaker 
* 3. Bluetooth connected: toggle phone/ speaker/ bluetooth 
* 
* @param tab 
*/ 
private void handleTabAudioSourceClick(TabLayout.Tab tab) { 
    View view = tab.getCustomView(); 
    ImageView icon = (ImageView) view.findViewById(R.id.imageViewIcon); 
    int currentAudioSourceIdentifier = (Integer) view.getTag(); 

    if (audioManager.isWiredHeadsetOn() == false && BluetoothManager.isBluetoothHeadsetConnected() == false) { 
     // No headphones and no bluetooth device: toggle phone/ speaker. 
     if (currentAudioSourceIdentifier == R.drawable.tab_speaker) { 
      // Current audio source is earpiece, moving to speaker. 
      view.setTag(android.R.drawable.stat_sys_speakerphone); 
      icon.setImageResource(android.R.drawable.stat_sys_speakerphone); 
      AudioSourceUtil.connectSpeaker(audioManager); 
     } else { 
      // Current audio source is speaker, moving to earpiece. 
      view.setTag(R.drawable.tab_speaker); 
      icon.setImageResource(R.drawable.tab_speaker); 
      AudioSourceUtil.connectEarpiece(audioManager); 
     } 
    } else if (audioManager.isWiredHeadsetOn()) { 
     // Headphones connected: toggle headphones/ speaker. 
     if (currentAudioSourceIdentifier == android.R.drawable.stat_sys_speakerphone) { 
      // Current audio source is speaker, moving to headphones. 
      view.setTag(android.R.drawable.stat_sys_headset); 
      icon.setImageResource(android.R.drawable.stat_sys_headset); 
      AudioSourceUtil.connectHeadphones(audioManager); 
     } else { 
      // Current audio source is headphones, moving to speaker. 
      view.setTag(android.R.drawable.stat_sys_speakerphone); 
      icon.setImageResource(android.R.drawable.stat_sys_speakerphone); 
      AudioSourceUtil.connectSpeaker(audioManager); 
     } 
    } else if (BluetoothManager.isBluetoothHeadsetConnected()) { 
     // Bluetooth connected: toggle phone/ speaker/ bluetooth. 
     if (currentAudioSourceIdentifier == R.drawable.tab_speaker) { 
      // Current audio source is earpiece, moving to speaker. 
      view.setTag(android.R.drawable.stat_sys_speakerphone); 
      icon.setImageResource(android.R.drawable.stat_sys_speakerphone); 
      AudioSourceUtil.connectSpeaker(audioManager); 
     } else if (currentAudioSourceIdentifier == android.R.drawable.stat_sys_speakerphone) { 
      // Current audio source is speaker, moving to bluetooth. 
      view.setTag(android.R.drawable.stat_sys_data_bluetooth); 
      icon.setImageResource(android.R.drawable.stat_sys_data_bluetooth); 
      AudioSourceUtil.connectBluetooth(audioManager); 
     } else { 
      // Current audio source is bluetooth, moving to earpiece. 
      view.setTag(R.drawable.tab_speaker); 
      icon.setImageResource(R.drawable.tab_speaker); 
      AudioSourceUtil.connectEarpiece(audioManager); 
     } 
    } 
} 
相關問題