AudioManagerCompat
@UnstableApi
public final class AudioManagerCompat
Compatibility layer for AudioManager with fallbacks for older Android versions.
Summary
Nested types |
|---|
@DocumentedAudio focus gain types. |
Constants |
|
|---|---|
static final int |
AUDIOFOCUS_GAIN = 1Used 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"). |
static final int |
AUDIOFOCUS_NONE = 0Used to indicate no audio focus has been gained or lost, or requested. |
Public methods |
|
|---|---|
static int |
abandonAudioFocusRequest(Abandon audio focus. |
synchronized static AudioManager |
getAudioManager(Context context)Returns the |
static @IntRange(from = 0) int |
getStreamMaxVolume(Returns the maximum volume index for a particular stream. |
static @IntRange(from = 0) int |
getStreamMinVolume(Returns the minimum volume index for a particular stream. |
static int |
getStreamVolume(AudioManager audioManager, @C.StreamType int streamType)Returns the current volume for a particular stream. |
static boolean |
isStreamMute(AudioManager audioManager, @C.StreamType int streamType)Returns whether the given stream is muted. |
static int |
requestAudioFocus(Requests audio focus. |
Constants
AUDIOFOCUS_GAIN
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
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
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
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.
AUDIOFOCUS_NONE
public static final int AUDIOFOCUS_NONE = 0
Used to indicate no audio focus has been gained or lost, or requested.
Public methods
abandonAudioFocusRequest
public static int abandonAudioFocusRequest(
AudioManager audioManager,
AudioFocusRequestCompat focusRequest
)
Abandon audio focus. Causes the previous focus owner, if any, to receive focus.
| Parameters | |
|---|---|
AudioManager audioManager |
The |
AudioFocusRequestCompat focusRequest |
The |
| Returns | |
|---|---|
int |
getAudioManager
synchronized public static AudioManager getAudioManager(Context context)
Returns the AudioManager.
This method avoids potential threading issues where AudioManager keeps access to the thread it was created on until after this thread is stopped.
It is recommended to use this method from a background thread.
| Returns | |
|---|---|
AudioManager |
The |
getStreamMaxVolume
public static @IntRange(from = 0) int getStreamMaxVolume(
AudioManager audioManager,
@C.StreamType int streamType
)
Returns the maximum volume index for a particular stream.
| Parameters | |
|---|---|
AudioManager audioManager |
The |
@C.StreamType int streamType |
The |
| Returns | |
|---|---|
@IntRange(from = 0) int |
The maximum valid volume index for the stream. |
getStreamMinVolume
public static @IntRange(from = 0) int getStreamMinVolume(
AudioManager audioManager,
@C.StreamType int streamType
)
Returns the minimum volume index for a particular stream.
| Parameters | |
|---|---|
AudioManager audioManager |
The |
@C.StreamType int streamType |
The |
| Returns | |
|---|---|
@IntRange(from = 0) int |
The minimum valid volume index for the stream. |
getStreamVolume
public static int getStreamVolume(AudioManager audioManager, @C.StreamType int streamType)
Returns the current volume for a particular stream.
| Parameters | |
|---|---|
AudioManager audioManager |
The |
@C.StreamType int streamType |
The |
| Returns | |
|---|---|
int |
The current volume of the stream. |
isStreamMute
public static boolean isStreamMute(AudioManager audioManager, @C.StreamType int streamType)
Returns whether the given stream is muted.
| Parameters | |
|---|---|
AudioManager audioManager |
The |
@C.StreamType int streamType |
The |
| Returns | |
|---|---|
boolean |
Whether the stream is muted. |
requestAudioFocus
public static int requestAudioFocus(
AudioManager audioManager,
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 | |
|---|---|
AudioManager audioManager |
The |
AudioFocusRequestCompat focusRequest |
An |
| Returns | |
|---|---|
int |