失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > Android判断当前耳机的连接状态(蓝牙 有线) 修改音频的输出方式

Android判断当前耳机的连接状态(蓝牙 有线) 修改音频的输出方式

时间:2019-03-06 06:25:11

相关推荐

Android判断当前耳机的连接状态(蓝牙 有线) 修改音频的输出方式

一,蓝牙设备的连接,在广播注册之前连接:

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 /11/2.*/public class HeadsetPlugReceiver extends BroadcastReceiver {private static final String TAG = "HeadsetPlugReceiver";private HeadsetPlugListener mHeadsetPlugListener;public HeadsetPlugReceiver(HeadsetPlugListener headsetPlugListener) {this.mHeadsetPlugListener = headsetPlugListener;}@Overridepublic 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_DISCONNECTED== 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() {@Overridepublic 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开启视频通话的时候,有线耳机、蓝牙耳机的连接与断开,发现如果同时连接,声音从最后发生改变的那个设备输出。

如果觉得《Android判断当前耳机的连接状态(蓝牙 有线) 修改音频的输出方式》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。