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 |
|---|
@DocumentedPlayer commands. |
public interface AudioFocusManager.PlayerControlInterface 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(Constructs an AudioFocusManager to automatically handle audio focus for a player. |
Public methods |
|
|---|---|
float |
Gets the current player volume multiplier. |
void |
release()Called when the manager is no longer required. |
void |
setAudioAttributes(@Nullable AudioAttributes audioAttributes)Sets audio attributes that should be used to manage audio focus. |
int |
@AudioFocusManager.PlayerCommandCalled 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
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 |
AudioFocusManager.PlayerControl playerControl |
A |
Public methods
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 |
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 |