AudioManagerCompat

public final class AudioManagerCompat


Compatibility library for AudioManager with fallbacks for older platforms.

Summary

Constants

static final int

Used to indicate a gain of audio focus, or a request of audio focus, of unknown duration.

static final int

Used to indicate a temporary gain or request of audio focus, anticipated to last a short amount of time.

static final int

Used to indicate a temporary request of audio focus, anticipated to last a short amount of time, during which no other applications, or system components, should play anything.

static final int

Used to indicate a temporary request of audio focus, anticipated to last a short amount of time, and where it is acceptable for other audio applications to keep playing after having lowered their output level (also referred to as "ducking").

Public methods

static int
abandonAudioFocusRequest(
    @NonNull AudioManager audioManager,
    @NonNull AudioFocusRequestCompat focusRequest
)

Abandon audio focus.

static @IntRange(from = 0) int
getStreamMaxVolume(@NonNull AudioManager audioManager, int streamType)

Returns the maximum volume index for a particular stream.

static @IntRange(from = 0) int
getStreamMinVolume(@NonNull AudioManager audioManager, int streamType)

Returns the minimum volume index for a particular stream.

static boolean

Indicates if the device implements a fixed volume policy.

static int
requestAudioFocus(
    @NonNull AudioManager audioManager,
    @NonNull AudioFocusRequestCompat focusRequest
)

Requests audio focus.

Constants

AUDIOFOCUS_GAIN

Added in 1.1.0
public static final int AUDIOFOCUS_GAIN = 1

Used to indicate a gain of audio focus, or a request of audio focus, of unknown duration.

AUDIOFOCUS_GAIN_TRANSIENT

Added in 1.1.0
public static final int AUDIOFOCUS_GAIN_TRANSIENT = 2

Used to indicate a temporary gain or request of audio focus, anticipated to last a short amount of time. Examples of temporary changes are the playback of driving directions, or an event notification.

AUDIOFOCUS_GAIN_TRANSIENT_EXCLUSIVE

Added in 1.1.0
public static final int AUDIOFOCUS_GAIN_TRANSIENT_EXCLUSIVE = 4

Used to indicate a temporary request of audio focus, anticipated to last a short amount of time, during which no other applications, or system components, should play anything. Examples of exclusive and transient audio focus requests are voice memo recording and speech recognition, during which the system shouldn't play any notifications, and media playback should have paused.

AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK

Added in 1.1.0
public static final int AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK = 3

Used to indicate a temporary request of audio focus, anticipated to last a short amount of time, and where it is acceptable for other audio applications to keep playing after having lowered their output level (also referred to as "ducking"). Examples of temporary changes are the playback of driving directions where playback of music in the background is acceptable.

Public methods

abandonAudioFocusRequest

Added in 1.1.0
public static int abandonAudioFocusRequest(
    @NonNull AudioManager audioManager,
    @NonNull AudioFocusRequestCompat focusRequest
)

Abandon audio focus. Causes the previous focus owner, if any, to receive focus.

Parameters
@NonNull AudioFocusRequestCompat focusRequest

the AudioFocusRequestCompat that was used when requesting focus with requestAudioFocus.

Throws
java.lang.IllegalArgumentException

if passed a null argument

getStreamMaxVolume

Added in 1.2.0
public static @IntRange(from = 0) int getStreamMaxVolume(@NonNull AudioManager audioManager, int streamType)

Returns the maximum volume index for a particular stream.

Parameters
int streamType

The stream type whose maximum volume index is returned.

Returns
@IntRange(from = 0) int

The maximum valid volume index for the stream.

getStreamMinVolume

Added in 1.2.0
public static @IntRange(from = 0) int getStreamMinVolume(@NonNull AudioManager audioManager, int streamType)

Returns the minimum volume index for a particular stream.

Parameters
int streamType

The stream type whose minimum volume index is returned.

Returns
@IntRange(from = 0) int

The minimum valid volume index for the stream.

isVolumeFixed

Added in 1.5.0
public static boolean isVolumeFixed(@NonNull AudioManager audioManager)

Indicates if the device implements a fixed volume policy.

Some devices may not have volume control and may operate at a fixed volume, and may not enable muting or changing the volume of audio streams. This method will return true on such devices.

Compatibility: It returns false on API level below 21 even if the device has fixed volume.

requestAudioFocus

Added in 1.1.0
public static int requestAudioFocus(
    @NonNull AudioManager audioManager,
    @NonNull AudioFocusRequestCompat focusRequest
)

Requests audio focus. See the AudioFocusRequestCompat for information about the options available to configure your request, and notification of focus gain and loss.

Parameters
@NonNull AudioFocusRequestCompat focusRequest

an AudioFocusRequestCompat instance used to configure how focus is requested.

Throws
java.lang.NullPointerException

if passed a null argument