Android 监听耳机外放设备

Android 设备插拔耳机状态监听;播放视频,音频切换(外放,耳机).

 

  核心代码:

 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;
 
 
/**
 * 耳机状态监听
 *  注册广播监听
 *
 */
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说明有耳机
    }
}

 

 

//耳机植入监听    1、具体使用的类调用
mHeadsetPlugReceiver = new HeadsetPlugReceiver(new HeadsetPlugReceiver.HeadsetPlugListener() {
    @Override
    public void onHeadsetPlug(boolean isPlug) {

// 接口回调        isPlug ----监听的状态
        if (isPlug) {
            if (BluetoothProfile.STATE_CONNECTED == adapter.getProfileConnectionState(BluetoothProfile.HEADSET)) {
                //蓝牙设备输出

              microSpeaker(context);
            } else if (BluetoothProfile.STATE_DISCONNECTED == adapter.getProfileConnectionState(BluetoothProfile.HEADSET)) {
                //外放

       loudSpeaker(context)
            } else {
                //外放

       loudSpeaker(context)
            }
        } else {
            //耳机输出

    microSpeaker(context)
        }
    }
});

// 2.注册广播 放在页面初始化的地方.
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(Intent.ACTION_HEADSET_PLUG);
intentFilter.addAction(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED);
registerReceiver(mHeadsetPlugReceiver, intentFilter);

 

// 3.处理音频外放,还是当前耳机播放 

//外放
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);
}

 

https://blog.csdn.net/huangwenkui1990/article/details/94554305

你可能感兴趣的:(Android 监听耳机外放设备)