一,藍牙設備的連接,在廣播註冊之前連接:
1. 判斷耳機的連接狀態,我們比較常用的是廣播的方式,但是在安卓8.0以後,如果耳機在註冊廣播之前連接,那麼在註冊廣播,無法監聽到耳機的狀態,於是我們只能換一種方式去處理,代碼如下:
AudioManager mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
mAudioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
//獲取當前使用的麥克風,設置媒體播放麥克風
if (mAudioManager.isWiredHeadsetOn()) {
//todo 有線耳機已連接,聲音內放,從耳機輸出
}else{
//todo 有線耳機未連接,聲音外放,
}
2. 那麼問題來了,如果連接了藍牙設備呢,顯然從廣播的方式處理已經解決不了問題:
if (BluetoothProfile.STATE_CONNECTED == adapter.getProfileConnectionState(BluetoothProfile.HEADSET)) {
//todo 藍牙設備已連接,聲音內放,從藍牙設備輸出
} else if (BluetoothProfile.STATE_DISCONNECTED == adapter.getProfileConnectionState(BluetoothProfile.HEADSET)) {
//todo 藍牙設備未連接,聲音外放,
} else {
//todo 藍牙設備未連接,聲音外放,
}
二,廣播註冊之後,耳機的連接狀態發生改變,通過廣播監聽即可:
廣播代碼:
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothHeadset;
import android.bluetooth.BluetoothProfile;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import tv.buka.roomSdk.util.LogUtil;
/**
* 耳機狀態監聽
* <p>
* Created by hwk on 2017/11/2.
*/
public class HeadsetPlugReceiver extends BroadcastReceiver {
private static final String TAG = "HeadsetPlugReceiver";
private HeadsetPlugListener mHeadsetPlugListener;
public HeadsetPlugReceiver(HeadsetPlugListener headsetPlugListener) {
this.mHeadsetPlugListener = headsetPlugListener;
}
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED.equals(action)) {
LogUtil.e(TAG, action);
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
int state = adapter.getProfileConnectionState(BluetoothProfile.HEADSET);
if (BluetoothProfile.STATE_CONNECTED == state) {
mHeadsetPlugListener.onHeadsetPlug(true);
}
if (BluetoothProfile.STATE_CONNECTED == state) {
mHeadsetPlugListener.onHeadsetPlug(false);
}
}
} else if (Intent.ACTION_HEADSET_PLUG.equals(action)) {
if (intent.hasExtra("state")) {
if (intent.getIntExtra("state", 0) == 0) {
//外放
mHeadsetPlugListener.onHeadsetPlug(true);
} else if (intent.getIntExtra("state", 0) == 1) {
//耳機
mHeadsetPlugListener.onHeadsetPlug(false);
}
}
}
}
public interface HeadsetPlugListener {
void onHeadsetPlug(boolean isPlug);//true說明沒有耳機 false說明有耳機
}
}
實現代碼:
//耳機植入監聽
mHeadsetPlugReceiver = new HeadsetPlugReceiver(new HeadsetPlugReceiver.HeadsetPlugListener() {
@Override
public void onHeadsetPlug(boolean isPlug) {
if (isPlug) {
if (BluetoothProfile.STATE_CONNECTED == adapter.getProfileConnectionState(BluetoothProfile.HEADSET)) {
//藍牙設備輸出
} else if (BluetoothProfile.STATE_DISCONNECTED == adapter.getProfileConnectionState(BluetoothProfile.HEADSET)) {
//外放
} else {
//外放
}
} else {
//耳機輸出
}
}
});
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(Intent.ACTION_HEADSET_PLUG);
intentFilter.addAction(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED);
registerReceiver(mHeadsetPlugReceiver, intentFilter);
三,聲音內放、和外放的處理
//外放
public void loudSpeaker(Activity context) {
AudioManager audioManager = (AudioManager)context.getSystemService("audio");
audioManager.setSpeakerphoneOn(true);
context.setVolumeControlStream(0);
audioManager.setMode(0);
}
//內放
public void microSpeaker(Activity context) {
AudioManager audioManager = (AudioManager)context.getSystemService("audio");
audioManager.setSpeakerphoneOn(false);
context.setVolumeControlStream(0);
audioManager.setMode(0);
}
總結:研究微信、qq開啓視頻通話的時候,有線耳機、藍牙耳機的連接與斷開,發現如果同時連接,聲音從最後發生改變的那個設備輸出。