# AudioManagerCompat

Artifact: [androidx.media3:media3-common](https://developer.android.com/jetpack/androidx/releases/media3) [View Source](https://cs.android.com/search?q=file:androidx/media3/common/audio/AudioManagerCompat.java+class:androidx.media3.common.audio.AudioManagerCompat)

*** ** * ** ***


```
@UnstableApi
public final class AudioManagerCompat
```

<br />

*** ** * ** ***

Compatibility layer for `https://developer.android.com/reference/android/media/AudioManager.html` with fallbacks for older Android versions.

## Summary

| ### Nested types |
|---|
| `@https://developer.android.com/reference/java/lang/annotation/Documented.html @https://developer.android.com/reference/java/lang/annotation/Retention.html(value = https://developer.android.com/reference/java/lang/annotation/RetentionPolicy.html#SOURCE) @https://developer.android.com/reference/java/lang/annotation/Target.html(value = https://developer.android.com/reference/java/lang/annotation/ElementType.html#TYPE_USE) @https://developer.android.com/reference/androidx/annotation/IntDef(value = ) public annotation https://developer.android.com/reference/androidx/media3/common/audio/AudioManagerCompat.AudioFocusGain` Audio focus gain types. |

| ### Constants |
|---|---|
| `static final int` | `https://developer.android.com/reference/androidx/media3/common/audio/AudioManagerCompat#AUDIOFOCUS_GAIN() = 1` Used to indicate a gain of audio focus, or a request of audio focus, of unknown duration. |
| `static final int` | `https://developer.android.com/reference/androidx/media3/common/audio/AudioManagerCompat#AUDIOFOCUS_GAIN_TRANSIENT() = 2` Used to indicate a temporary gain or request of audio focus, anticipated to last a short amount of time. |
| `static final int` | `https://developer.android.com/reference/androidx/media3/common/audio/AudioManagerCompat#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. |
| `static final int` | `https://developer.android.com/reference/androidx/media3/common/audio/AudioManagerCompat#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"). |
| `static final int` | `https://developer.android.com/reference/androidx/media3/common/audio/AudioManagerCompat#AUDIOFOCUS_NONE() = 0` Used to indicate no audio focus has been gained or lost, or requested. |

| ### Public methods |
|---|---|
| `static int` | `https://developer.android.com/reference/androidx/media3/common/audio/AudioManagerCompat#abandonAudioFocusRequest(android.media.AudioManager,androidx.media3.common.audio.AudioFocusRequestCompat)( https://developer.android.com/reference/android/media/AudioManager.html audioManager, https://developer.android.com/reference/androidx/media3/common/audio/AudioFocusRequestCompat focusRequest )` Abandon audio focus. |
| `synchronized static https://developer.android.com/reference/android/media/AudioManager.html` | `https://developer.android.com/reference/androidx/media3/common/audio/AudioManagerCompat#getAudioManager(android.content.Context)(https://developer.android.com/reference/android/content/Context.html context)` Returns the `https://developer.android.com/reference/android/media/AudioManager.html`. |
| `static @https://developer.android.com/reference/androidx/annotation/IntRange(from = 0) int` | `https://developer.android.com/reference/androidx/media3/common/audio/AudioManagerCompat#getStreamMaxVolume(android.media.AudioManager,int)( https://developer.android.com/reference/android/media/AudioManager.html audioManager, @https://developer.android.com/reference/androidx/media3/common/C.StreamType int streamType )` Returns the maximum volume index for a particular stream. |
| `static @https://developer.android.com/reference/androidx/annotation/IntRange(from = 0) int` | `https://developer.android.com/reference/androidx/media3/common/audio/AudioManagerCompat#getStreamMinVolume(android.media.AudioManager,int)( https://developer.android.com/reference/android/media/AudioManager.html audioManager, @https://developer.android.com/reference/androidx/media3/common/C.StreamType int streamType )` Returns the minimum volume index for a particular stream. |
| `static int` | `https://developer.android.com/reference/androidx/media3/common/audio/AudioManagerCompat#getStreamVolume(android.media.AudioManager,int)(https://developer.android.com/reference/android/media/AudioManager.html audioManager, @https://developer.android.com/reference/androidx/media3/common/C.StreamType int streamType)` Returns the current volume for a particular stream. |
| `static boolean` | `https://developer.android.com/reference/androidx/media3/common/audio/AudioManagerCompat#isStreamMute(android.media.AudioManager,int)(https://developer.android.com/reference/android/media/AudioManager.html audioManager, @https://developer.android.com/reference/androidx/media3/common/C.StreamType int streamType)` Returns whether the given stream is muted. |
| `static int` | `https://developer.android.com/reference/androidx/media3/common/audio/AudioManagerCompat#requestAudioFocus(android.media.AudioManager,androidx.media3.common.audio.AudioFocusRequestCompat)( https://developer.android.com/reference/android/media/AudioManager.html audioManager, https://developer.android.com/reference/androidx/media3/common/audio/AudioFocusRequestCompat focusRequest )` 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 |
|---|---|
| `https://developer.android.com/reference/android/media/AudioManager.html audioManager` | The `https://developer.android.com/reference/android/media/AudioManager.html`. |
| `https://developer.android.com/reference/androidx/media3/common/audio/AudioFocusRequestCompat focusRequest` | The `https://developer.android.com/reference/androidx/media3/common/audio/AudioFocusRequestCompat` that was used when requesting focus with `https://developer.android.com/reference/androidx/media3/common/audio/AudioManagerCompat#requestAudioFocus(android.media.AudioManager,androidx.media3.common.audio.AudioFocusRequestCompat)`. |

| Returns |
|---|---|
| `int` | `https://developer.android.com/reference/android/media/AudioManager.html#AUDIOFOCUS_REQUEST_FAILED` or `https://developer.android.com/reference/android/media/AudioManager.html#AUDIOFOCUS_REQUEST_GRANTED` |

### getAudioManager

```
synchronized public static AudioManager getAudioManager(Context context)
```

Returns the `https://developer.android.com/reference/android/media/AudioManager.html`.

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.

| Parameters |
|---|---|
| `https://developer.android.com/reference/android/content/Context.html context` | A `https://developer.android.com/reference/android/content/Context.html`. |

| Returns |
|---|---|
| `https://developer.android.com/reference/android/media/AudioManager.html` | The `https://developer.android.com/reference/android/media/AudioManager.html`. |

### getStreamMaxVolume

```
public static @IntRange(from = 0) int getStreamMaxVolume(
    AudioManager audioManager,
    @C.StreamType int streamType
)
```

Returns the maximum volume index for a particular stream.

| Parameters |
|---|---|
| `https://developer.android.com/reference/android/media/AudioManager.html audioManager` | The `https://developer.android.com/reference/android/media/AudioManager.html`. |
| `@https://developer.android.com/reference/androidx/media3/common/C.StreamType int streamType` | The `https://developer.android.com/reference/androidx/media3/common/C.StreamType` whose maximum volume index is returned. |

| Returns |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/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 |
|---|---|
| `https://developer.android.com/reference/android/media/AudioManager.html audioManager` | The `https://developer.android.com/reference/android/media/AudioManager.html`. |
| `@https://developer.android.com/reference/androidx/media3/common/C.StreamType int streamType` | The `https://developer.android.com/reference/androidx/media3/common/C.StreamType` whose minimum volume index is returned. |

| Returns |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/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 |
|---|---|
| `https://developer.android.com/reference/android/media/AudioManager.html audioManager` | The `https://developer.android.com/reference/android/media/AudioManager.html`. |
| `@https://developer.android.com/reference/androidx/media3/common/C.StreamType int streamType` | The `https://developer.android.com/reference/androidx/media3/common/C.StreamType` whose volume is returned. |

| 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 |
|---|---|
| `https://developer.android.com/reference/android/media/AudioManager.html audioManager` | The `https://developer.android.com/reference/android/media/AudioManager.html`. |
| `@https://developer.android.com/reference/androidx/media3/common/C.StreamType int streamType` | The `https://developer.android.com/reference/androidx/media3/common/C.StreamType` to check. |

| Returns |
|---|---|
| `boolean` | Whether the stream is muted. |

### requestAudioFocus

```
public static int requestAudioFocus(
    AudioManager audioManager,
    AudioFocusRequestCompat focusRequest
)
```

Requests audio focus. See the `https://developer.android.com/reference/androidx/media3/common/audio/AudioFocusRequestCompat` for information about the options available to configure your request, and notification of focus gain and loss.

| Parameters |
|---|---|
| `https://developer.android.com/reference/android/media/AudioManager.html audioManager` | The `https://developer.android.com/reference/android/media/AudioManager.html`. |
| `https://developer.android.com/reference/androidx/media3/common/audio/AudioFocusRequestCompat focusRequest` | An `https://developer.android.com/reference/androidx/media3/common/audio/AudioFocusRequestCompat` instance used to configure how focus is requested. |

| Returns |
|---|---|
| `int` | `https://developer.android.com/reference/android/media/AudioManager.html#AUDIOFOCUS_REQUEST_FAILED`, `https://developer.android.com/reference/android/media/AudioManager.html#AUDIOFOCUS_REQUEST_GRANTED` or `https://developer.android.com/reference/android/media/AudioManager.html#AUDIOFOCUS_REQUEST_DELAYED`. |