下面列出了android.media.AudioManager#isBluetoothA2dpOn ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void execute(AudioManager audioManager) {
if (voiceMediator.isBlueToothHeadSet()) {
if (!voiceMediator.isSuportA2DP()) {
if (audioManager.getMode() != AudioManager.MODE_NORMAL) {
Log.e(TAG, "playInChannel>>setMode(AudioManager.MODE_NORMAL)");
audioManager.setMode(AudioManager.MODE_NORMAL);
}
if (audioManager.isBluetoothScoOn()) {
audioManager.setBluetoothScoOn(false);
audioManager.stopBluetoothSco();
}
} else {
if (!audioManager.isBluetoothA2dpOn()) {
Log.e(TAG, "playInChannel>>setBluetoothA2dpOn(true)");
audioManager.setBluetoothA2dpOn(true);
}
}
}
}
public int Init(Context context, String apkPath, int nFlag) {
m_hHandler = new msgHandler();
m_context = context;
AudioManager am = (AudioManager)context.getSystemService(context.AUDIO_SERVICE);
if (am != null && am.isBluetoothA2dpOn())
m_nBTOffset = 250;
m_strApkPath = apkPath;
m_pObjPlayer = new WeakReference<MediaPlayer>(this);
m_NativeContext = nativeInit(m_pObjPlayer, apkPath, nFlag);
if (m_NativeContext == 0)
return -1;
if (m_NativeSurface != null)
nativeSetView(m_NativeContext, m_NativeSurface);
return 0;
}
@SuppressLint("MissingPermission")
public synchronized boolean isAudioOutputAvailable() {
Context context = contextWeakReference.get();
if(context == null){ return false;}
AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
// If API level 23+ audio manager can iterate over all current devices to see if a supported
// device is present.
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
AudioDeviceInfo[] deviceInfos = audioManager.getDevices(AudioManager.GET_DEVICES_OUTPUTS);
if(deviceInfos != null) {
for (AudioDeviceInfo deviceInfo : deviceInfos) {
if (deviceInfo != null && isSupportedAudioDevice(deviceInfo.getType())) {
return true;
}
}
}
return false;
}
//This means the SDK version is < M, and our min is 8 so this API is always available
return audioManager.isBluetoothA2dpOn();
}
/**
* 检查蓝牙 A2DP 音频外设是否已连接
* @return {@code true} yes, {@code false} no
*/
public static boolean isBluetoothA2dpOn() {
AudioManager audioManager = AppUtils.getAudioManager();
if (audioManager != null) {
try {
return audioManager.isBluetoothA2dpOn();
} catch (Exception e) {
LogPrintUtils.eTag(TAG, e, "isBluetoothA2dpOn");
}
}
return false;
}
public static boolean downMixAudio() {
AudioManager am = (AudioManager) TvApp.getApplication().getSystemService(Context.AUDIO_SERVICE);
if (am.isBluetoothA2dpOn()) {
Timber.i("Downmixing audio due to wired headset");
return true;
}
return (DeviceUtils.isFireTv() && !DeviceUtils.is50()) || TvApp.getApplication().getUserPreferences().get(UserPreferences.Companion.getAudioBehaviour()) == AudioBehavior.DOWNMIX_TO_STEREO;
}
public AudioRender(Context context, BasePlayer player) {
mPlayer = player;
mAudioTrack = null;
AudioManager am = (AudioManager)context.getSystemService(context.AUDIO_SERVICE);
if (am != null && am.isBluetoothA2dpOn())
mOffset = 250;
Log.v (TAG, "Audio Manager, offset " + mOffset);
}
@Override
public void onSensorChanged(SensorEvent event) {
AudioManager audioManager = (AudioManager) mContext.getSystemService(AUDIO_SERVICE);
try {
if (audioManager.isBluetoothA2dpOn() || audioManager.isWiredHeadsetOn()) {
return;
}
if (mAdapter.getMediaPlayer().isPlaying()) {
float distance = event.values[0];
if (distance >= mSensor.getMaximumRange()) {
mAdapter.setAudioPlayByEarPhone(0);
setScreenOn();
} else {
mAdapter.setAudioPlayByEarPhone(2);
ViewHolderController.getInstance().replayVoice();
setScreenOff();
}
} else {
if (mWakeLock != null && mWakeLock.isHeld()) {
mWakeLock.release();
mWakeLock = null;
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onSensorChanged(SensorEvent event) {
AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
try {
if (audioManager.isBluetoothA2dpOn() || audioManager.isWiredHeadsetOn()) {
return;
}
if (mAdapter.getMediaPlayer().isPlaying()) {
float distance = event.values[0];
if (distance >= mSensor.getMaximumRange()) {
mAdapter.setAudioPlayByEarPhone(0);
setScreenOn();
} else {
mAdapter.setAudioPlayByEarPhone(2);
ViewHolderController.getInstance().replayVoice();
setScreenOff();
}
} else {
if (mWakeLock != null && mWakeLock.isHeld()) {
mWakeLock.release();
mWakeLock = null;
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
private boolean getHeadphoneStatus() {
AudioManager audioManager = (AudioManager)getSystemService(AUDIO_SERVICE);
boolean headphone = audioManager.isWiredHeadsetOn() || audioManager.isBluetoothA2dpOn();
return headphone;
}
@ReactMethod(isBlockingSynchronousMethod = true)
public boolean isHeadphonesConnectedSync() {
AudioManager audioManager = (AudioManager)getReactApplicationContext().getSystemService(Context.AUDIO_SERVICE);
return audioManager.isWiredHeadsetOn() || audioManager.isBluetoothA2dpOn();
}