/** * Calls into AudioSystem to check the current status of audio streams. * * <p>The inPastMs value is only used by the system on API 11+. * * @param stream The stream ID to query * @param inPastMs The time interval allowance, in milliseconds, for checking active streams * @return {@code true} if the stream is or was open in the past {@code inPastMs} milliseconds, * {@code false} otherwise */ public static boolean isStreamActive(int stream, int inPastMs) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { return (Boolean) CompatUtils.invoke(null, false, METHOD_isStreamActive, stream, inPastMs); } else { return (Boolean) CompatUtils.invoke(null, false, METHOD_LEGACY_isStreamActive, stream); } }
public class AudioSystemCompatUtils { private static final Class<?> CLASS_AudioSystem = CompatUtils.getClass("android.media.AudioSystem"); private static final Method METHOD_isStreamActive = CompatUtils.getMethod(CLASS_AudioSystem, "isStreamActive", int.class, int.class); private static final Method METHOD_LEGACY_isStreamActive = CompatUtils.getMethod(CLASS_AudioSystem, "isStreamActive", int.class); private static final Method METHOD_isSourceActive = CompatUtils.getMethod(CLASS_AudioSystem, "isSourceActive", int.class); /** * Calls into AudioSystem to check the current status of audio streams. * * <p>The inPastMs value is only used by the system on API 11+. * * @param stream The stream ID to query * @param inPastMs The time interval allowance, in milliseconds, for checking active streams * @return {@code true} if the stream is or was open in the past {@code inPastMs} milliseconds, * {@code false} otherwise */ public static boolean isStreamActive(int stream, int inPastMs) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { return (Boolean) CompatUtils.invoke(null, false, METHOD_isStreamActive, stream, inPastMs); } else { return (Boolean) CompatUtils.invoke(null, false, METHOD_LEGACY_isStreamActive, stream); } } /** * Calls into AudioSystem to check the current status of an input source. * * <p>This is only available on API 17+ and will always return false if invoked on earlier * platforms. * * @param source The source ID to query. Expects constants from {@code MediaRecorder.AudioSource} * @return {@code true} if the input source is active, {@code false} otherwise. */ public static boolean isSourceActive(int source) { return (Boolean) CompatUtils.invoke(null, false, METHOD_isSourceActive, source); } }
/** * Calls into AudioSystem to check the current status of an input source. * * <p>This is only available on API 17+ and will always return false if invoked on earlier * platforms. * * @param source The source ID to query. Expects constants from {@code MediaRecorder.AudioSource} * @return {@code true} if the input source is active, {@code false} otherwise. */ public static boolean isSourceActive(int source) { return (Boolean) CompatUtils.invoke(null, false, METHOD_isSourceActive, source); }