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

*** ** * ** ***


```
@UnstableApi
public final 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 `https://developer.android.com/reference/android/os/Looper.html` for the background thread used for accessing the class.

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

| ### Constants |
|---|---|
| `static final int` | `https://developer.android.com/reference/androidx/media3/common/audio/AudioFocusManager#PLAYER_COMMAND_DO_NOT_PLAY() = -1` Do not play, because audio focus is lost or denied. |
| `static final int` | `https://developer.android.com/reference/androidx/media3/common/audio/AudioFocusManager#PLAYER_COMMAND_PLAY_WHEN_READY() = 1` Play freely, because audio focus is granted or not applicable. |
| `static final int` | `https://developer.android.com/reference/androidx/media3/common/audio/AudioFocusManager#PLAYER_COMMAND_WAIT_FOR_CALLBACK() = 0` Do not play now, because of a transient focus loss. |

| ### Public fields |
|---|---|
| `float` | `https://developer.android.com/reference/androidx/media3/common/audio/AudioFocusManager#volumeMultiplier()` |

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

| ### Public methods |
|---|---|
| `float` | `https://developer.android.com/reference/androidx/media3/common/audio/AudioFocusManager#getVolumeMultiplier()()` Gets the current player volume multiplier. |
| `void` | `https://developer.android.com/reference/androidx/media3/common/audio/AudioFocusManager#release()()` Called when the manager is no longer required. |
| `void` | `https://developer.android.com/reference/androidx/media3/common/audio/AudioFocusManager#setAudioAttributes(androidx.media3.common.AudioAttributes)(@https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/androidx/media3/common/AudioAttributes audioAttributes)` Sets audio attributes that should be used to manage audio focus. |
| `int` | `@https://developer.android.com/reference/androidx/media3/common/audio/AudioFocusManager.PlayerCommand https://developer.android.com/reference/androidx/media3/common/audio/AudioFocusManager#updateAudioFocus(boolean,int)(boolean playWhenReady, @https://developer.android.com/reference/androidx/media3/common/Player.State int playbackState)` Called by the player to abandon or request audio focus based on the desired player state. |

## Constants

### PLAYER_COMMAND_DO_NOT_PLAY

```
public static final int PLAYER_COMMAND_DO_NOT_PLAY = -1
```

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

### PLAYER_COMMAND_PLAY_WHEN_READY

```
public static final int PLAYER_COMMAND_PLAY_WHEN_READY = 1
```

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

### PLAYER_COMMAND_WAIT_FOR_CALLBACK

```
public static final int PLAYER_COMMAND_WAIT_FOR_CALLBACK = 0
```

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

## Public fields

### volumeMultiplier

```
public float volumeMultiplier
```

## Public constructors

### AudioFocusManager

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

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

| Parameters |
|---|---|
| `https://developer.android.com/reference/android/content/Context.html context` | The current context. |
| `https://developer.android.com/reference/android/os/Looper.html eventLooper` | A `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 `https://developer.android.com/reference/android/os/Looper.html` to avoid calling blocking system calls on the main thread. |
| `https://developer.android.com/reference/androidx/media3/common/audio/AudioFocusManager.PlayerControl playerControl` | A `https://developer.android.com/reference/androidx/media3/common/audio/AudioFocusManager.PlayerControl` to handle commands from this instance. |

## Public methods

### getVolumeMultiplier

```
public float getVolumeMultiplier()
```

Gets the current player volume multiplier.

### release

```
public void release()
```

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

### setAudioAttributes

```
public void setAudioAttributes(@Nullable AudioAttributes audioAttributes)
```

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

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

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/androidx/media3/common/AudioAttributes audioAttributes` | The audio attributes or `null` if audio focus should not be managed automatically. |

### updateAudioFocus

```
@AudioFocusManager.PlayerCommand
public int updateAudioFocus(boolean playWhenReady, @Player.State int playbackState)
```

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

| Parameters |
|---|---|
| `boolean playWhenReady` | The desired value of playWhenReady. |
| `@https://developer.android.com/reference/androidx/media3/common/Player.State int playbackState` | The desired playback state. |

| Returns |
|---|---|
| `int` | A `https://developer.android.com/reference/androidx/media3/common/audio/AudioFocusManager.PlayerCommand` to execute on the player. |