AudioManagerCompat

class AudioManagerCompat


Compatibility library for AudioManager with fallbacks for older platforms.

Summary

Constants

const Int

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

const Int

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

const 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.

const 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 functions

java-static Int
abandonAudioFocusRequest(
    audioManager: AudioManager,
    focusRequest: AudioFocusRequestCompat
)

Abandon audio focus.

java-static @IntRange(from = 0) Int
getStreamMaxVolume(audioManager: AudioManager, streamType: Int)

Returns the maximum volume index for a particular stream.

java-static @IntRange(from = 0) Int
getStreamMinVolume(audioManager: AudioManager, streamType: Int)

Returns the minimum volume index for a particular stream.

java-static Boolean
isVolumeFixed(audioManager: AudioManager)

Indicates if the device implements a fixed volume policy.

java-static Int
requestAudioFocus(
    audioManager: AudioManager,
    focusRequest: AudioFocusRequestCompat
)

Requests audio focus.

Constants

AUDIOFOCUS_GAIN

Added in 1.1.0
const val AUDIOFOCUS_GAIN = 1: Int

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
const val AUDIOFOCUS_GAIN_TRANSIENT = 2: Int

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
const val AUDIOFOCUS_GAIN_TRANSIENT_EXCLUSIVE = 4: 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. 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
const val AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK = 3: 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"). Examples of temporary changes are the playback of driving directions where playback of music in the background is acceptable.

Public functions

abandonAudioFocusRequest

Added in 1.1.0
java-static fun abandonAudioFocusRequest(
    audioManager: AudioManager,
    focusRequest: AudioFocusRequestCompat
): Int

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

Parameters
focusRequest: AudioFocusRequestCompat

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
java-static fun getStreamMaxVolume(audioManager: AudioManager, streamType: Int): @IntRange(from = 0) Int

Returns the maximum volume index for a particular stream.

Parameters
streamType: Int

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
java-static fun getStreamMinVolume(audioManager: AudioManager, streamType: Int): @IntRange(from = 0) Int

Returns the minimum volume index for a particular stream.

Parameters
streamType: Int

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
java-static fun isVolumeFixed(audioManager: AudioManager): Boolean

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
java-static fun requestAudioFocus(
    audioManager: AudioManager,
    focusRequest: AudioFocusRequestCompat
): Int

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

Parameters
focusRequest: AudioFocusRequestCompat

an AudioFocusRequestCompat instance used to configure how focus is requested.

Throws
java.lang.NullPointerException

if passed a null argument