AudioFocusManager


@UnstableApi
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

const Int

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

const Int

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

const Int

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

Public constructors

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

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

Public functions

Unit

Called when the manager is no longer required.

Unit

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

Int

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

Public properties

Float

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!

The current context.

eventLooper: Looper!

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.

playerControl: AudioFocusManager.PlayerControl!

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

setAudioAttributes

fun setAudioAttributes(audioAttributes: AudioAttributes?): Unit

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

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

Parameters
audioAttributes: 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

The desired value of playWhenReady.

@Player.State playbackState: Int

The desired playback state.

Returns
Int

A PlayerCommand to execute on the player.

Public properties

volumeMultiplier

val volumeMultiplierFloat