AudioFocusRequestCompat.Builder


class AudioFocusRequestCompat.Builder


Builder class for AudioFocusRequestCompat objects.

The default values are:

  • focus listener and handler: none
  • audio attributes: DEFAULT
  • pauses on duck: false
  • supports delayed focus grant: false

In contrast to a AudioFocusRequest, attempting to build an without an AudioManager.OnAudioFocusChangeListener will throw an IllegalArgumentException, because the listener is required for all API levels up to API 26.

Summary

Public constructors

Constructs a new Builder, and specifies how audio focus will be requested.

Public functions

AudioFocusRequestCompat!

Builds a new AudioFocusRequestCompat instance combining all the information gathered by this builder's configuration methods.

AudioFocusRequestCompat.Builder!

Sets the AudioAttributes to be associated with the focus request, and which describe the use case for which focus is requested.

AudioFocusRequestCompat.Builder!

Sets the type of AudioManagerCompat.AudioFocusGain that will be requested.

AudioFocusRequestCompat.Builder!

Sets the listener called when audio focus changes after being requested with requestAudioFocus, and until being abandoned with abandonAudioFocusRequest.

AudioFocusRequestCompat.Builder!

Sets the listener called when audio focus changes after being requested with requestAudioFocus, and until being abandoned with abandonAudioFocusRequest.

AudioFocusRequestCompat.Builder!

Declares the intended behavior of the application with regards to audio ducking.

Public constructors

Builder

Builder(@AudioManagerCompat.AudioFocusGain focusGain: Int)

Constructs a new Builder, and specifies how audio focus will be requested.

By default there is no focus change listener, delayed focus is not supported, ducking is suitable for the application, and the AudioAttributes are set to DEFAULT.

Parameters
@AudioManagerCompat.AudioFocusGain focusGain: Int

The type of AudioManagerCompat.AudioFocusGain that will be requested.

Public functions

build

fun build(): AudioFocusRequestCompat!

Builds a new AudioFocusRequestCompat instance combining all the information gathered by this builder's configuration methods.

Returns
AudioFocusRequestCompat!

The AudioFocusRequestCompat.

setAudioAttributes

@CanIgnoreReturnValue
fun setAudioAttributes(attributes: AudioAttributes!): AudioFocusRequestCompat.Builder!

Sets the AudioAttributes to be associated with the focus request, and which describe the use case for which focus is requested. As the focus requests typically precede audio playback, this information is used on certain platforms to declare the subsequent playback use case. It is therefore good practice to use in this method the same AudioAttributes as used for playback, see for example ExoPlayer.Builder.setAudioAttributes().

Parameters
attributes: AudioAttributes!

The AudioAttributes for the focus request.

Returns
AudioFocusRequestCompat.Builder!

This Builder instance.

setFocusGain

@CanIgnoreReturnValue
fun setFocusGain(@AudioManagerCompat.AudioFocusGain focusGain: Int): AudioFocusRequestCompat.Builder!

Sets the type of AudioManagerCompat.AudioFocusGain that will be requested.

Parameters
@AudioManagerCompat.AudioFocusGain focusGain: Int

The type of AudioManagerCompat.AudioFocusGain that will be requested.

Returns
AudioFocusRequestCompat.Builder!

This Builder instance.

setOnAudioFocusChangeListener

@CanIgnoreReturnValue
fun setOnAudioFocusChangeListener(
    listener: AudioManager.OnAudioFocusChangeListener!
): AudioFocusRequestCompat.Builder!

Sets the listener called when audio focus changes after being requested with requestAudioFocus, and until being abandoned with abandonAudioFocusRequest. Note that only focus changes (gains and losses) affecting the focus owner are reported, not gains and losses of other focus requesters in the system. Notifications are delivered on the main thread.

Parameters
listener: AudioManager.OnAudioFocusChangeListener!

The AudioManager.OnAudioFocusChangeListener receiving the focus change notifications.

Returns
AudioFocusRequestCompat.Builder!

This Builder instance.

setOnAudioFocusChangeListener

@CanIgnoreReturnValue
fun setOnAudioFocusChangeListener(
    listener: AudioManager.OnAudioFocusChangeListener!,
    handler: Handler!
): AudioFocusRequestCompat.Builder!

Sets the listener called when audio focus changes after being requested with requestAudioFocus, and until being abandoned with abandonAudioFocusRequest. Note that only focus changes (gains and losses) affecting the focus owner are reported, not gains and losses of other focus requesters in the system.

Parameters
listener: AudioManager.OnAudioFocusChangeListener!

The AudioManager.OnAudioFocusChangeListener receiving the focus change notifications.

handler: Handler!

The Handler for the thread on which to execute the notifications.

Returns
AudioFocusRequestCompat.Builder!

This Builder instance.

setWillPauseWhenDucked

@CanIgnoreReturnValue
fun setWillPauseWhenDucked(pauseOnDuck: Boolean): AudioFocusRequestCompat.Builder!

Declares the intended behavior of the application with regards to audio ducking. See more details in the AudioFocusRequest class documentation. Setting pauseOnDuck to true will only have an effect on O and later.

Parameters
pauseOnDuck: Boolean

Use true if the application intends to pause audio playback when losing focus with AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK.

Returns
AudioFocusRequestCompat.Builder!

This Builder instance.