# AudioManagerCompat

# 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)

*** ** * ** ***

Kotlin\|[Java](https://developer.android.com/reference/androidx/media3/common/audio/AudioManagerCompat "View this page in Java")

<br />

```
@UnstableApi
class AudioManagerCompat
```

<br />

*** ** * ** ***

Compatibility layer for[AudioManager](https://developer.android.com/reference/android/media/AudioManager.html)with fallbacks for older Android versions.

## Summary

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

|                                    ### Constants                                    |
|-------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `const `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) | [AUDIOFOCUS_GAIN](https://developer.android.com/reference/kotlin/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.                                                                                                                                                                                               |
| `const `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) | [AUDIOFOCUS_GAIN_TRANSIENT](https://developer.android.com/reference/kotlin/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.                                                                                                                                                            |
| `const `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) | [AUDIOFOCUS_GAIN_TRANSIENT_EXCLUSIVE](https://developer.android.com/reference/kotlin/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.                                                                |
| `const `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) | [AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK](https://developer.android.com/reference/kotlin/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"). |
| `const `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) | [AUDIOFOCUS_NONE](https://developer.android.com/reference/kotlin/androidx/media3/common/audio/AudioManagerCompat#AUDIOFOCUS_NONE())` = 0` Used to indicate no audio focus has been gained or lost, or requested.                                                                                                                                                                                                                  |

|                                                                                      ### Public functions                                                                                      |
|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `java-static `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)                                                                                                      | [abandonAudioFocusRequest](https://developer.android.com/reference/kotlin/androidx/media3/common/audio/AudioManagerCompat#abandonAudioFocusRequest(android.media.AudioManager,androidx.media3.common.audio.AudioFocusRequestCompat))`(` ` audioManager: `[AudioManager](https://developer.android.com/reference/android/media/AudioManager.html)`!,` ` focusRequest: `[AudioFocusRequestCompat](https://developer.android.com/reference/kotlin/androidx/media3/common/audio/AudioFocusRequestCompat)`!` `)` Abandon audio focus.                       |
| `synchronized java-static `[AudioManager](https://developer.android.com/reference/android/media/AudioManager.html)`!`                                                                          | [getAudioManager](https://developer.android.com/reference/kotlin/androidx/media3/common/audio/AudioManagerCompat#getAudioManager(android.content.Context))`(context: `[Context](https://developer.android.com/reference/android/content/Context.html)`!)` Returns the[AudioManager](https://developer.android.com/reference/android/media/AudioManager.html).                                                                                                                                                                                          |
| `java-static @`[IntRange](https://developer.android.com/reference/kotlin/androidx/annotation/IntRange)`(from = 0) `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) | [getStreamMaxVolume](https://developer.android.com/reference/kotlin/androidx/media3/common/audio/AudioManagerCompat#getStreamMaxVolume(android.media.AudioManager,int))`(` ` audioManager: `[AudioManager](https://developer.android.com/reference/android/media/AudioManager.html)`!,` ` @`[C.StreamType](https://developer.android.com/reference/kotlin/androidx/media3/common/C.StreamType)` streamType: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) `)` Returns the maximum volume index for a particular stream. |
| `java-static @`[IntRange](https://developer.android.com/reference/kotlin/androidx/annotation/IntRange)`(from = 0) `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) | [getStreamMinVolume](https://developer.android.com/reference/kotlin/androidx/media3/common/audio/AudioManagerCompat#getStreamMinVolume(android.media.AudioManager,int))`(` ` audioManager: `[AudioManager](https://developer.android.com/reference/android/media/AudioManager.html)`!,` ` @`[C.StreamType](https://developer.android.com/reference/kotlin/androidx/media3/common/C.StreamType)` streamType: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) `)` Returns the minimum volume index for a particular stream. |
| `java-static `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)                                                                                                      | [getStreamVolume](https://developer.android.com/reference/kotlin/androidx/media3/common/audio/AudioManagerCompat#getStreamVolume(android.media.AudioManager,int))`(audioManager: `[AudioManager](https://developer.android.com/reference/android/media/AudioManager.html)`!, @`[C.StreamType](https://developer.android.com/reference/kotlin/androidx/media3/common/C.StreamType)` streamType: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)`)` Returns the current volume for a particular stream.                     |
| `java-static `[Boolean](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html)                                                                                              | [isStreamMute](https://developer.android.com/reference/kotlin/androidx/media3/common/audio/AudioManagerCompat#isStreamMute(android.media.AudioManager,int))`(audioManager: `[AudioManager](https://developer.android.com/reference/android/media/AudioManager.html)`!, @`[C.StreamType](https://developer.android.com/reference/kotlin/androidx/media3/common/C.StreamType)` streamType: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)`)` Returns whether the given stream is muted.                                    |
| `java-static `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)                                                                                                      | [requestAudioFocus](https://developer.android.com/reference/kotlin/androidx/media3/common/audio/AudioManagerCompat#requestAudioFocus(android.media.AudioManager,androidx.media3.common.audio.AudioFocusRequestCompat))`(` ` audioManager: `[AudioManager](https://developer.android.com/reference/android/media/AudioManager.html)`!,` ` focusRequest: `[AudioFocusRequestCompat](https://developer.android.com/reference/kotlin/androidx/media3/common/audio/AudioFocusRequestCompat)`!` `)` Requests audio focus.                                    |

## Constants

### AUDIOFOCUS_GAIN

```
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

```
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

```
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

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

### AUDIOFOCUS_NONE

```
const val AUDIOFOCUS_NONE = 0: Int
```

Used to indicate no audio focus has been gained or lost, or requested.  

## Public functions

### abandonAudioFocusRequest

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

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

|                                                                    Parameters                                                                     |
|---------------------------------------------------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `audioManager: `[AudioManager](https://developer.android.com/reference/android/media/AudioManager.html)`!`                                        | The[AudioManager](https://developer.android.com/reference/android/media/AudioManager.html).                                                                                                                                                                                                                                                                                                      |
| `focusRequest: `[AudioFocusRequestCompat](https://developer.android.com/reference/kotlin/androidx/media3/common/audio/AudioFocusRequestCompat)`!` | The[AudioFocusRequestCompat](https://developer.android.com/reference/kotlin/androidx/media3/common/audio/AudioFocusRequestCompat)that was used when requesting focus with[requestAudioFocus](https://developer.android.com/reference/kotlin/androidx/media3/common/audio/AudioManagerCompat#requestAudioFocus(android.media.AudioManager,androidx.media3.common.audio.AudioFocusRequestCompat)). |

|                                   Returns                                   |
|-----------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) | [AUDIOFOCUS_REQUEST_FAILED](https://developer.android.com/reference/android/media/AudioManager.html#AUDIOFOCUS_REQUEST_FAILED())or[AUDIOFOCUS_REQUEST_GRANTED](https://developer.android.com/reference/android/media/AudioManager.html#AUDIOFOCUS_REQUEST_GRANTED()) |

### getAudioManager

```
synchronized java-static fun getAudioManager(context: Context!): AudioManager!
```

Returns the[AudioManager](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                                           |
|-----------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------|
| `context: `[Context](https://developer.android.com/reference/android/content/Context.html)`!` | A[Context](https://developer.android.com/reference/android/content/Context.html). |

|                                          Returns                                           |
|--------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------|
| [AudioManager](https://developer.android.com/reference/android/media/AudioManager.html)`!` | The[AudioManager](https://developer.android.com/reference/android/media/AudioManager.html). |

### getStreamMaxVolume

```
java-static fun getStreamMaxVolume(
    audioManager: AudioManager!,
    @C.StreamType streamType: Int
): @IntRange(from = 0) Int
```

Returns the maximum volume index for a particular stream.  

|                                                                                           Parameters                                                                                            |
|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------|
| `audioManager: `[AudioManager](https://developer.android.com/reference/android/media/AudioManager.html)`!`                                                                                      | The[AudioManager](https://developer.android.com/reference/android/media/AudioManager.html).                                                  |
| `@`[C.StreamType](https://developer.android.com/reference/kotlin/androidx/media3/common/C.StreamType)` streamType: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) | The[C.StreamType](https://developer.android.com/reference/kotlin/androidx/media3/common/C.StreamType)whose maximum volume index is returned. |

|                                                                                      Returns                                                                                       |
|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|------------------------------------------------|
| `@`[IntRange](https://developer.android.com/reference/kotlin/androidx/annotation/IntRange)`(from = 0) `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) | The maximum valid volume index for the stream. |

### getStreamMinVolume

```
java-static fun getStreamMinVolume(
    audioManager: AudioManager!,
    @C.StreamType streamType: Int
): @IntRange(from = 0) Int
```

Returns the minimum volume index for a particular stream.  

|                                                                                           Parameters                                                                                            |
|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------|
| `audioManager: `[AudioManager](https://developer.android.com/reference/android/media/AudioManager.html)`!`                                                                                      | The[AudioManager](https://developer.android.com/reference/android/media/AudioManager.html).                                                  |
| `@`[C.StreamType](https://developer.android.com/reference/kotlin/androidx/media3/common/C.StreamType)` streamType: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) | The[C.StreamType](https://developer.android.com/reference/kotlin/androidx/media3/common/C.StreamType)whose minimum volume index is returned. |

|                                                                                      Returns                                                                                       |
|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|------------------------------------------------|
| `@`[IntRange](https://developer.android.com/reference/kotlin/androidx/annotation/IntRange)`(from = 0) `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) | The minimum valid volume index for the stream. |

### getStreamVolume

```
java-static fun getStreamVolume(audioManager: AudioManager!, @C.StreamType streamType: Int): Int
```

Returns the current volume for a particular stream.  

|                                                                                           Parameters                                                                                            |
|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------|
| `audioManager: `[AudioManager](https://developer.android.com/reference/android/media/AudioManager.html)`!`                                                                                      | The[AudioManager](https://developer.android.com/reference/android/media/AudioManager.html).                                    |
| `@`[C.StreamType](https://developer.android.com/reference/kotlin/androidx/media3/common/C.StreamType)` streamType: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) | The[C.StreamType](https://developer.android.com/reference/kotlin/androidx/media3/common/C.StreamType)whose volume is returned. |

|                                   Returns                                   |
|-----------------------------------------------------------------------------|-----------------------------------|
| [Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) | The current volume of the stream. |

### isStreamMute

```
java-static fun isStreamMute(audioManager: AudioManager!, @C.StreamType streamType: Int): Boolean
```

Returns whether the given stream is muted.  

|                                                                                           Parameters                                                                                            |
|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------|
| `audioManager: `[AudioManager](https://developer.android.com/reference/android/media/AudioManager.html)`!`                                                                                      | The[AudioManager](https://developer.android.com/reference/android/media/AudioManager.html).                    |
| `@`[C.StreamType](https://developer.android.com/reference/kotlin/androidx/media3/common/C.StreamType)` streamType: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) | The[C.StreamType](https://developer.android.com/reference/kotlin/androidx/media3/common/C.StreamType)to check. |

|                                       Returns                                       |
|-------------------------------------------------------------------------------------|------------------------------|
| [Boolean](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html) | Whether the stream is muted. |

### requestAudioFocus

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

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

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

|                                   Returns                                   |
|-----------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) | [AUDIOFOCUS_REQUEST_FAILED](https://developer.android.com/reference/android/media/AudioManager.html#AUDIOFOCUS_REQUEST_FAILED())or[AUDIOFOCUS_REQUEST_GRANTED](https://developer.android.com/reference/android/media/AudioManager.html#AUDIOFOCUS_REQUEST_GRANTED()). |