# AudioFocusManager

# AudioFocusManager

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/AudioFocusManager.java+class:androidx.media3.common.audio.AudioFocusManager)

*** ** * ** ***

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

<br />

```
@UnstableApi
class AudioFocusManager
```

<br />

*** ** * ** ***

Manages requesting and responding to changes in audio focus.

This class should be used on a single background thread to avoid blocking system calls. The constructor can be called on any thread, but has to specify the[Looper](https://developer.android.com/reference/android/os/Looper.html)for the background thread used for accessing the class.

## 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 `[AudioFocusManager.PlayerCommand](https://developer.android.com/reference/kotlin/androidx/media3/common/audio/AudioFocusManager.PlayerCommand) Player commands. |
| `interface `[AudioFocusManager.PlayerControl](https://developer.android.com/reference/kotlin/androidx/media3/common/audio/AudioFocusManager.PlayerControl) Interface to allow AudioFocusManager to give commands to a player.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               |

|                                    ### Constants                                    |
|-------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `const `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) | [PLAYER_COMMAND_DO_NOT_PLAY](https://developer.android.com/reference/kotlin/androidx/media3/common/audio/AudioFocusManager#PLAYER_COMMAND_DO_NOT_PLAY())` = -1` Do not play, because audio focus is lost or denied.                   |
| `const `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) | [PLAYER_COMMAND_PLAY_WHEN_READY](https://developer.android.com/reference/kotlin/androidx/media3/common/audio/AudioFocusManager#PLAYER_COMMAND_PLAY_WHEN_READY())` = 1` Play freely, because audio focus is granted or not applicable. |
| `const `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) | [PLAYER_COMMAND_WAIT_FOR_CALLBACK](https://developer.android.com/reference/kotlin/androidx/media3/common/audio/AudioFocusManager#PLAYER_COMMAND_WAIT_FOR_CALLBACK())` = 0` Do not play now, because of a transient focus loss.        |

|                                                                                                                                                                                                                                                                                                                                        ### Public constructors                                                                                                                                                                                                                                                                                                                                         |
|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [AudioFocusManager](https://developer.android.com/reference/kotlin/androidx/media3/common/audio/AudioFocusManager#AudioFocusManager(android.content.Context,android.os.Looper,androidx.media3.common.audio.AudioFocusManager.PlayerControl))`(` ` context: `[Context](https://developer.android.com/reference/android/content/Context.html)`!,` ` eventLooper: `[Looper](https://developer.android.com/reference/android/os/Looper.html)`!,` ` playerControl: `[AudioFocusManager.PlayerControl](https://developer.android.com/reference/kotlin/androidx/media3/common/audio/AudioFocusManager.PlayerControl)`!` `)` Constructs an AudioFocusManager to automatically handle audio focus for a player. |

|                             ### Public functions                              |
|-------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html) | [release](https://developer.android.com/reference/kotlin/androidx/media3/common/audio/AudioFocusManager#release())`()` Called when the manager is no longer required.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   |
| [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html) | [setAudioAttributes](https://developer.android.com/reference/kotlin/androidx/media3/common/audio/AudioFocusManager#setAudioAttributes(androidx.media3.common.AudioAttributes))`(audioAttributes: `[AudioAttributes](https://developer.android.com/reference/kotlin/androidx/media3/common/AudioAttributes)`?)` Sets audio attributes that should be used to manage audio focus.                                                                                                                                                                                                                                                                                                                         |
| [Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)   | `@`[AudioFocusManager.PlayerCommand](https://developer.android.com/reference/kotlin/androidx/media3/common/audio/AudioFocusManager.PlayerCommand) [updateAudioFocus](https://developer.android.com/reference/kotlin/androidx/media3/common/audio/AudioFocusManager#updateAudioFocus(boolean,int))`(playWhenReady: `[Boolean](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html)`, @`[Player.State](https://developer.android.com/reference/kotlin/androidx/media3/common/Player.State)` playbackState: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)`)` Called by the player to abandon or request audio focus based on the desired player state. |

|                              ### Public properties                              |
|---------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------|
| [Float](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html) | [volumeMultiplier](https://developer.android.com/reference/kotlin/androidx/media3/common/audio/AudioFocusManager#volumeMultiplier()) |

## Constants

### PLAYER_COMMAND_DO_NOT_PLAY

```
const val PLAYER_COMMAND_DO_NOT_PLAY = -1: Int
```

Do not play, because audio focus is lost or denied.  

### PLAYER_COMMAND_PLAY_WHEN_READY

```
const val PLAYER_COMMAND_PLAY_WHEN_READY = 1: Int
```

Play freely, because audio focus is granted or not applicable.  

### PLAYER_COMMAND_WAIT_FOR_CALLBACK

```
const val PLAYER_COMMAND_WAIT_FOR_CALLBACK = 0: Int
```

Do not play now, because of a transient focus loss.  

## Public constructors

### AudioFocusManager

```
AudioFocusManager(
    context: Context!,
    eventLooper: Looper!,
    playerControl: AudioFocusManager.PlayerControl!
)
```

Constructs an AudioFocusManager to automatically handle audio focus for a player.  

|                                                                             Parameters                                                                             |
|--------------------------------------------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `context: `[Context](https://developer.android.com/reference/android/content/Context.html)`!`                                                                      | The current context.                                                                                                                                                                                                                                                                            |
| `eventLooper: `[Looper](https://developer.android.com/reference/android/os/Looper.html)`!`                                                                         | A[Looper](https://developer.android.com/reference/android/os/Looper.html)for the thread on which the audio focus manager is used. This should be a background[Looper](https://developer.android.com/reference/android/os/Looper.html)to avoid calling blocking system calls on the main thread. |
| `playerControl: `[AudioFocusManager.PlayerControl](https://developer.android.com/reference/kotlin/androidx/media3/common/audio/AudioFocusManager.PlayerControl)`!` | A[PlayerControl](https://developer.android.com/reference/kotlin/androidx/media3/common/audio/AudioFocusManager.PlayerControl)to handle commands from this instance.                                                                                                                             |

## Public functions

### release

```
fun release(): Unit
```

Called when the manager is no longer required. Audio focus will be released without making any calls to the[PlayerControl](https://developer.android.com/reference/kotlin/androidx/media3/common/audio/AudioFocusManager.PlayerControl).  

### setAudioAttributes

```
fun setAudioAttributes(audioAttributes: AudioAttributes?): Unit
```

Sets audio attributes that should be used to manage audio focus.

Call[updateAudioFocus](https://developer.android.com/reference/kotlin/androidx/media3/common/audio/AudioFocusManager#updateAudioFocus(boolean,int))to update the audio focus based on these attributes.  

|                                                           Parameters                                                           |
|--------------------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------|
| `audioAttributes: `[AudioAttributes](https://developer.android.com/reference/kotlin/androidx/media3/common/AudioAttributes)`?` | The audio attributes or`null`if audio focus should not be managed automatically. |

### updateAudioFocus

```
@AudioFocusManager.PlayerCommand
fun updateAudioFocus(playWhenReady: Boolean, @Player.State playbackState: Int): Int
```

Called by the player to abandon or request audio focus based on the desired player state.  

|                                                                                             Parameters                                                                                             |
|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-------------------------------------|
| `playWhenReady: `[Boolean](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html)                                                                                               | The desired value of playWhenReady. |
| `@`[Player.State](https://developer.android.com/reference/kotlin/androidx/media3/common/Player.State)` playbackState: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) | The desired playback state.         |

|                                   Returns                                   |
|-----------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------|
| [Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) | A[PlayerCommand](https://developer.android.com/reference/kotlin/androidx/media3/common/audio/AudioFocusManager.PlayerCommand)to execute on the player. |

## Public properties

### volumeMultiplier

```
val volumeMultiplier: Float
```