Java Code Examples for android.media.AudioManager#getDevices()

The following examples show how to use android.media.AudioManager#getDevices() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MainActivity.java    From wear-os-samples with Apache License 2.0 6 votes vote down vote up
/**
 * Determines if the wear device has a built-in speaker and if it is supported. Speaker, even if
 * physically present, is only supported in Android M+ on a wear device..
 */
public final boolean speakerIsSupported() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        PackageManager packageManager = 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) 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;
}
 
Example 2
Source File: HeadsetTracker.java    From Easer with GNU General Public License v3.0 6 votes vote down vote up
HeadsetTracker(Context context, HeadsetUSourceData data,
               @NonNull PendingIntent event_positive,
               @NonNull PendingIntent event_negative) {
    super(context, data, event_positive, event_negative);

    boolean plugged_in = false;
    Boolean has_microphone = null;

    AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
        AudioDeviceInfo[] audioDevices = audioManager.getDevices(AudioManager.GET_DEVICES_ALL);
        for (AudioDeviceInfo deviceInfo : audioDevices) {
            if (deviceInfo.getType() == AudioDeviceInfo.TYPE_WIRED_HEADPHONES
                    || deviceInfo.getType() == AudioDeviceInfo.TYPE_WIRED_HEADSET) {
                plugged_in = true;
                has_microphone = deviceInfo.isSource();
            }
        }
    } else {
        plugged_in = audioManager.isWiredHeadsetOn();
    }
    Boolean match = determine_match(data, plugged_in, has_microphone);
    newSatisfiedState(match);
}
 
Example 3
Source File: MediaStreamingStatus.java    From sdl_java_suite with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@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();
}
 
Example 4
Source File: WebRtcAudioUtils.java    From webrtc_android with MIT License 5 votes vote down vote up
private static void logAudioDeviceInfo(String tag, AudioManager audioManager) {
  if (Build.VERSION.SDK_INT < 23) {
    return;
  }
  final AudioDeviceInfo[] devices =
      audioManager.getDevices(AudioManager.GET_DEVICES_ALL);
  if (devices.length == 0) {
    return;
  }
  Logging.d(tag, "Audio Devices: ");
  for (AudioDeviceInfo device : devices) {
    StringBuilder info = new StringBuilder();
    info.append("  ").append(deviceTypeToString(device.getType()));
    info.append(device.isSource() ? "(in): " : "(out): ");
    // An empty array indicates that the device supports arbitrary channel counts.
    if (device.getChannelCounts().length > 0) {
      info.append("channels=").append(Arrays.toString(device.getChannelCounts()));
      info.append(", ");
    }
    if (device.getEncodings().length > 0) {
      // Examples: ENCODING_PCM_16BIT = 2, ENCODING_PCM_FLOAT = 4.
      info.append("encodings=").append(Arrays.toString(device.getEncodings()));
      info.append(", ");
    }
    if (device.getSampleRates().length > 0) {
      info.append("sample rates=").append(Arrays.toString(device.getSampleRates()));
      info.append(", ");
    }
    info.append("id=").append(device.getId());
    Logging.d(tag, info.toString());
  }
}
 
Example 5
Source File: WebRtcAudioUtils.java    From webrtc_android with MIT License 5 votes vote down vote up
private static void logAudioDeviceInfo(String tag, AudioManager audioManager) {
  if (Build.VERSION.SDK_INT < 23) {
    return;
  }
  final AudioDeviceInfo[] devices = audioManager.getDevices(AudioManager.GET_DEVICES_ALL);
  if (devices.length == 0) {
    return;
  }
  Logging.d(tag, "Audio Devices: ");
  for (AudioDeviceInfo device : devices) {
    StringBuilder info = new StringBuilder();
    info.append("  ").append(deviceTypeToString(device.getType()));
    info.append(device.isSource() ? "(in): " : "(out): ");
    // An empty array indicates that the device supports arbitrary channel counts.
    if (device.getChannelCounts().length > 0) {
      info.append("channels=").append(Arrays.toString(device.getChannelCounts()));
      info.append(", ");
    }
    if (device.getEncodings().length > 0) {
      // Examples: ENCODING_PCM_16BIT = 2, ENCODING_PCM_FLOAT = 4.
      info.append("encodings=").append(Arrays.toString(device.getEncodings()));
      info.append(", ");
    }
    if (device.getSampleRates().length > 0) {
      info.append("sample rates=").append(Arrays.toString(device.getSampleRates()));
      info.append(", ");
    }
    info.append("id=").append(device.getId());
    Logging.d(tag, info.toString());
  }
}
 
Example 6
Source File: AssistantActivity.java    From androidthings-googleassistant with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 7
Source File: AssistantActivity.java    From androidthings-googleassistant with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 8
Source File: AssistantActivity.java    From androidthings-googleassistant with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 9
Source File: AssistantActivity.java    From androidthings-googleassistant with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 10
Source File: AssistantActivity.java    From sample-googleassistant with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 11
Source File: HeadphoneStateMonitor.java    From talkback with Apache License 2.0 5 votes vote down vote up
/** Initializes this HeadphoneStateMonitor to start listening to headphone state changes. */
public void startMonitoring() {
  AudioManager audioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
  // Initialize the active device count.
  mConnectedAudioDevices.clear();
  AudioDeviceInfo[] devices = audioManager.getDevices(AudioManager.GET_DEVICES_OUTPUTS);
  for (AudioDeviceInfo device : devices) {
    if (isExternalDevice(device)) {
      mConnectedAudioDevices.add(device.getId());
    }
  }
  audioManager.registerAudioDeviceCallback(mAudioDeviceCallback, /* use the main thread */ null);
}
 
Example 12
Source File: HeadphoneStateMonitor.java    From talkback with Apache License 2.0 5 votes vote down vote up
/**
 * Whether the device is currently connected to bluetooth or wired headphones for audio output.
 * When called on older devices this use a deprecat methods on audioManager to get the same
 * result.
 */
@SuppressWarnings("deprecation")
public static boolean isHeadphoneOn(Context context) {
  AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
  AudioDeviceInfo[] devices = audioManager.getDevices(AudioManager.GET_DEVICES_OUTPUTS);
  for (AudioDeviceInfo device : devices) {
    if (isExternalDevice(device)) {
      // While there can be more than one external audio device, finding one is enough here.
      return true;
    }
  }
  return false;
}