105 lines
3.6 KiB
Java
105 lines
3.6 KiB
Java
package com.phxh.audio;
|
|
|
|
import android.content.Context;
|
|
import android.media.AudioManager;
|
|
import android.media.AudioDeviceInfo;
|
|
import android.os.Build;
|
|
|
|
public class AndroidAudioDeviceChecker {
|
|
private static AudioManager mAudioManager = null;
|
|
private static int lastKnownDeviceType = -1;
|
|
|
|
public static void init(Context context) {
|
|
if (mAudioManager == null) {
|
|
mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
|
|
updateLastKnownDevice();
|
|
}
|
|
}
|
|
|
|
public static boolean hasAudioDeviceChanged() {
|
|
if (mAudioManager == null) return false;
|
|
|
|
int currentDevice = getCurrentDeviceType();
|
|
boolean isChanged = (currentDevice != lastKnownDeviceType);
|
|
|
|
if (isChanged) {
|
|
lastKnownDeviceType = currentDevice;
|
|
}
|
|
|
|
return isChanged;
|
|
}
|
|
|
|
private static void updateLastKnownDevice() {
|
|
if (mAudioManager != null) {
|
|
lastKnownDeviceType = getCurrentDeviceType();
|
|
}
|
|
}
|
|
|
|
private static int getCurrentDeviceType() {
|
|
if (mAudioManager == null) return -1;
|
|
|
|
int deviceType = 0;
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
|
AudioDeviceInfo[] devices = mAudioManager.getDevices(AudioManager.GET_DEVICES_OUTPUTS);
|
|
for (AudioDeviceInfo device : devices) {
|
|
int type = device.getType();
|
|
if (type == AudioDeviceInfo.TYPE_WIRED_HEADPHONES ||
|
|
type == AudioDeviceInfo.TYPE_WIRED_HEADSET) {
|
|
deviceType |= 1;
|
|
} else if (type == AudioDeviceInfo.TYPE_BLUETOOTH_A2DP ||
|
|
type == AudioDeviceInfo.TYPE_BLUETOOTH_SCO) {
|
|
deviceType |= 2;
|
|
} else if (type == AudioDeviceInfo.TYPE_BUILTIN_SPEAKER) {
|
|
deviceType |= 4;
|
|
}
|
|
}
|
|
} else {
|
|
if (mAudioManager.isWiredHeadsetOn()) {
|
|
deviceType |= 1;
|
|
}
|
|
if (mAudioManager.isBluetoothA2dpOn() || mAudioManager.isBluetoothScoOn()) {
|
|
deviceType |= 2;
|
|
}
|
|
}
|
|
|
|
return deviceType;
|
|
}
|
|
|
|
public static boolean isWiredHeadsetConnected() {
|
|
if (mAudioManager == null) return false;
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
|
AudioDeviceInfo[] devices = mAudioManager.getDevices(AudioManager.GET_DEVICES_OUTPUTS);
|
|
for (AudioDeviceInfo device : devices) {
|
|
int type = device.getType();
|
|
if (type == AudioDeviceInfo.TYPE_WIRED_HEADPHONES ||
|
|
type == AudioDeviceInfo.TYPE_WIRED_HEADSET) {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
} else {
|
|
return mAudioManager.isWiredHeadsetOn();
|
|
}
|
|
}
|
|
|
|
public static boolean isBluetoothConnected() {
|
|
if (mAudioManager == null) return false;
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
|
AudioDeviceInfo[] devices = mAudioManager.getDevices(AudioManager.GET_DEVICES_OUTPUTS);
|
|
for (AudioDeviceInfo device : devices) {
|
|
int type = device.getType();
|
|
if (type == AudioDeviceInfo.TYPE_BLUETOOTH_A2DP ||
|
|
type == AudioDeviceInfo.TYPE_BLUETOOTH_SCO) {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
} else {
|
|
return mAudioManager.isBluetoothA2dpOn() || mAudioManager.isBluetoothScoOn();
|
|
}
|
|
}
|
|
}
|