耳机监听
项目中有监听耳有监听耳机使用时长的需求 ,根据需求首先要判断是否有耳机连接,耳机又分为了有线耳机和无线耳机两种 ,所以只要解决了耳机插拔的监听, 在插入耳机时开始计时, 耳机拔掉后停止计时 ,并且随时可以判断当前是否有耳机连接,问题就基本解决了
- 自定义耳机监听广播
public abstract class HeadsetReceiver extends BroadcastReceiver {
private boolean mIsRegistered = false;s
private Context mContext;
public synchronized void register(Context context) {
if (mIsRegistered) {
return;
}
mIsRegistered = true;
mContext = context;
registeUserPresentReceiver();
}
public void registeUserPresentReceiver() {
IntentFilter filter = new IntentFilter();
//检测是否插入耳机
filter.addAction(Intent.ACTION_HEADSET_PLUG);//有线耳机
filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);//蓝牙状态变化
filter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED);//蓝牙链接
filter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);//断开
filter.addAction("android.media.VOLUME_CHANGED_ACTION");//音量变化
mContext.registerReceiver(this, filter);
}
public synchronized void unregister() {
if (!mIsRegistered) {
return;
}
mIsRegistered = false;
mContext.unregisterReceiver(this);
mContext = null;
}
protected abstract void onPackageChanged(Intent intent);
@Override
public final void onReceive(Context context, Intent intent) {
onPackageChanged(intent);
}
}
- 注册广播
headsetReceiver.register(context);
- 接受到广播后处理
private HeadsetReceiver headsetReceiver = new HeadsetReceiver() {
@Override
protected void onPackageChanged(Intent intent) {
if (intent == null) {
Log.w(TAG, "onReceive, input intent is null !!!");
return;
}
String action = intent.getAction();
if (action == null || action.isEmpty()) {
Log.w(TAG, "onReceive, action is null or empty !!!");
return;
}
Log.d(TAG, "onReceive, action=" + action);
if (intent.getAction().equals(Intent.ACTION_HEADSET_PLUG)) {
if (intent.getIntExtra("state", 0) == 0) {
//外放
} else if (intent.getIntExtra("state", 0) == 1) {
//耳机
}
} else if (intent.getAction().equals(BluetoothDevice.ACTION_ACL_CONNECTED)) {
//蓝牙耳机连接
} else if (intent.getAction().equals(BluetoothDevice.ACTION_ACL_DISCONNECTED)) {
//蓝牙耳机拔出
} else if (intent.getAction().equals("android.media.VOLUME_CHANGED_ACTION")) {
//声音变化监听
}
}
};
- 判断耳机时是否连接工具类
public boolean getHeadSetStatus() {
boolean headSetStatus = false;
AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); // 获取声音管理器
if (audioManager.isWiredHeadsetOn()) { // 有线耳机是否连接
headSetStatus = true;
}
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); // 蓝牙耳机
if (bluetoothAdapter != null) {
if (bluetoothAdapter.isEnabled()) {
int a2dp = bluetoothAdapter.getProfileConnectionState(BluetoothProfile.A2DP); // 可操控蓝牙设备,如带播放暂停功能的蓝牙耳机
int headset = bluetoothAdapter.getProfileConnectionState(BluetoothProfile.HEADSET); // 蓝牙头戴式耳机,支持语音输入输出
//int health = bluetoothAdapter.getProfileConnectionState(BluetoothProfile.HEALTH); // 蓝牙穿戴式设备
// 查看是否蓝牙是否连接到三种设备的一种,以此来判断是否处于连接状态还是打开并没有连接的状态
if (a2dp == BluetoothProfile.STATE_CONNECTED) {
headSetStatus = true;
} else if (headset == BluetoothProfile.STATE_CONNECTED) {
headSetStatus = true;
}
}
}
return headSetStatus ;
}
- 常见输出设备标识
标识 | 设备 |
---|---|
DEVICE_OUT_EARPIECE = 0x1 | 听筒 |
DEVICE_OUT_SPEAKER = 0x2 | 扬声器 |
DEVICE_OUT_WIRED_HEADSET = 0x4 | 线控耳机,能够经过耳机控制远端播放、暂停、音量调节等功能的耳机 |
DEVICE_OUT_WIRED_HEADPHONE = 0x8 | 普通耳机,只能听,不能操控播放 |
DEVICE_OUT_BLUETOOTH_SCO = 0x10 | 单声道蓝牙耳机 蓝牙 SCO,用于语音通话,十进制32 |
DEVICE_OUT_BLUETOOTH_SCO_HEADSET = 0x20 | 蓝牙SCO耳机,十进制64 |
DEVICE_OUT_BLUETOOTH_SCO_CARKIT = 0x40 | 蓝牙SCO车载 |
DEVICE_OUT_BLUETOOTH_A2DP = 0x80 | 蓝牙高保真设备,用于听音乐, 十进制128 |
DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES = 0x100 | 蓝牙高保真耳机,十进制256 |
DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER = 0x200 | 蓝牙高保真扬声器 ,十进制512 |
DEVICE_OUT_AUX_DIGITAL 0x400 | 辅助数字输出,HDMI,十进制1024 |
DEVICE_OUT_ANLG_DOCK_HEADSET 0x800 | 经过基座链接的模拟有线耳机,十进制2048 |
DEVICE_OUT_DGTL_DOCK_HEADSET 0x1000 | 经过基座链接的数字有线耳机,十进制4096 |