/** * Checks whether a wired headset is connected or not. * This is not a valid indication that audio playback is actually over * the wired headset as audio routing depends on other conditions. We * only use it as an early indicator (during initialization) of an attached * wired headset. */ @Deprecated private boolean hasWiredHeadset() { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) { return audioManager.isWiredHeadsetOn(); } else { final AudioDeviceInfo[] devices = audioManager.getDevices(android.media.AudioManager.GET_DEVICES_ALL); for (AudioDeviceInfo device : devices) { final int type = device.getType(); if (type == AudioDeviceInfo.TYPE_WIRED_HEADSET) { Log.d(TAG, "hasWiredHeadset: found wired headset"); return true; } else if (type == AudioDeviceInfo.TYPE_USB_DEVICE) { Log.d(TAG, "hasWiredHeadset: found USB audio device"); return true; } } return false; } }
/** * Checks whether a wired headset is connected or not. * This is not a valid indication that audio playback is actually over * the wired headset as audio routing depends on other conditions. We * only use it as an early indicator (during initialization) of an attached * wired headset. */ @Deprecated private boolean hasWiredHeadset() { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) { return audioManager.isWiredHeadsetOn(); } else { final AudioDeviceInfo[] devices = audioManager.getDevices(AudioManager.GET_DEVICES_ALL); for (AudioDeviceInfo device : devices) { final int type = device.getType(); if (type == AudioDeviceInfo.TYPE_WIRED_HEADSET) { Log.d(TAG, "hasWiredHeadset: found wired headset"); return true; } else if (type == AudioDeviceInfo.TYPE_USB_DEVICE) { Log.d(TAG, "hasWiredHeadset: found USB audio device"); return true; } } return false; } }
/** * Used to get if the device has a speacker * @return */ public boolean hasSpeacker() { PackageManager packageManager = this.getPackageManager(); AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE); // Check whether the device has a speaker. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { // Check FEATURE_AUDIO_OUTPUT to guard against false positives. if (!packageManager.hasSystemFeature(PackageManager.FEATURE_AUDIO_OUTPUT)) { return false; } AudioDeviceInfo[] devices = audioManager.getDevices(AudioManager.GET_DEVICES_OUTPUTS); for (AudioDeviceInfo device : devices) { if (device.getType() == AudioDeviceInfo.TYPE_BUILTIN_SPEAKER) { return true; } } } return false; }
/** * Determines if the wear device has a built-in speaker or not. * <p> * <p><b>Important: </b>This method should only be called on a wear device; the return value on * a non-wear device can be trusted if and only if the device is running android version M+. */ public static final boolean wearHasSpeaker(Context context) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { PackageManager packageManager = context.getPackageManager(); // The results from AudioManager.getDevices can't be trusted unless the device // advertises FEATURE_AUDIO_OUTPUT. if (!packageManager.hasSystemFeature(PackageManager.FEATURE_AUDIO_OUTPUT)) { return false; } AudioManager audioManager = (AudioManager) context .getSystemService(Context.AUDIO_SERVICE); AudioDeviceInfo[] devices = audioManager.getDevices(AudioManager.GET_DEVICES_OUTPUTS); for (AudioDeviceInfo device : devices) { if (device.getType() == AudioDeviceInfo.TYPE_BUILTIN_SPEAKER) { return true; } } } return false; }
private AudioDeviceInfo findAudioDevice(int deviceFlag, int deviceType) { AudioManager manager = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE); AudioDeviceInfo[] adis = manager.getDevices(deviceFlag); for (AudioDeviceInfo adi : adis) { if (adi.getType() == deviceType) { return adi; } } return null; }
/** * Sets a preferred {@link AudioDeviceInfo} device for input. * * @param device The preferred audio device to acquire audio from. * @return Returns this builder to allow for chaining. */ public Builder setAudioInputDevice(AudioDeviceInfo device) { mEmbeddedAssistant.mAudioInputDevice = device; return this; }
/** * Sets a preferred {@link AudioDeviceInfo} device for output. * * param device The preferred audio device to route audio to. * @return Returns this builder to allow for chaining. */ public Builder setAudioOutputDevice(AudioDeviceInfo device) { mEmbeddedAssistant.mAudioOutputDevice = device; return this; }