AudioFocusManager


@UnstableApi
public final class AudioFocusManager


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 for the background thread used for accessing the class.

Summary

Nested types

Player commands.

Interface to allow AudioFocusManager to give commands to a player.

Constants

static final int

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

static final int

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

static final int

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

Public fields

float

Public constructors

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

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

Public methods

float

Gets the current player volume multiplier.

void

Called when the manager is no longer required.

void

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

int
@AudioFocusManager.PlayerCommand
updateAudioFocus(boolean playWhenReady, @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
Context context

The current context.

Looper eventLooper

A Looper for the thread on which the audio focus manager is used. This should be a background Looper to avoid calling blocking system calls on the main thread.

AudioFocusManager.PlayerControl playerControl

A 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 PlayerControl.

setAudioAttributes

public void setAudioAttributes(@Nullable AudioAttributes audioAttributes)

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

Call updateAudioFocus to update the audio focus based on these attributes.

Parameters
@Nullable 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.

@Player.State int playbackState

The desired playback state.

Returns
int

A PlayerCommand to execute on the player.