public final class

AudioFocusRequestCompat.Builder

extends java.lang.Object

 java.lang.Object

↳androidx.media.AudioFocusRequestCompat.Builder

Overview

Builder class for AudioFocusRequestCompat objects.

See AudioFocusRequestCompat for an example of building an instance with this builder.
The default values for the instance to be built are:

focus listener and handlernone
AudioAttributesCompatattributes with usage set to AudioAttributesCompat.USAGE_MEDIA
pauses on duckfalse
supports delayed focus grantfalse

In contrast to a , attempting to AudioFocusRequestCompat.Builder.build() an AudioFocusRequestCompat without an will throw an java.lang.IllegalArgumentException, because the listener is required for all API levels up to API 26.

Summary

Constructors
publicBuilder(AudioFocusRequestCompat requestToCopy)

Constructs a new Builder with all the properties of the AudioFocusRequestCompat passed as parameter.

publicBuilder(int focusGain)

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

Methods
public AudioFocusRequestCompatbuild()

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

public AudioFocusRequestCompat.BuildersetAudioAttributes(AudioAttributesCompat attributes)

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

public AudioFocusRequestCompat.BuildersetFocusGain(int focusGain)

Sets the type of focus gain that will be requested.

public AudioFocusRequestCompat.BuildersetOnAudioFocusChangeListener(OnAudioFocusChangeListener listener)

Sets the listener called when audio focus changes after being requested with AudioManagerCompat.requestAudioFocus(AudioManager, AudioFocusRequestCompat), and until being abandoned with AudioManagerCompat.abandonAudioFocusRequest(AudioManager, AudioFocusRequestCompat).

public AudioFocusRequestCompat.BuildersetOnAudioFocusChangeListener(OnAudioFocusChangeListener listener, Handler handler)

Sets the listener called when audio focus changes after being requested with AudioManagerCompat.requestAudioFocus(AudioManager, AudioFocusRequestCompat), and until being abandoned with AudioManagerCompat.abandonAudioFocusRequest(AudioManager, AudioFocusRequestCompat).

public AudioFocusRequestCompat.BuildersetWillPauseWhenDucked(boolean pauseOnDuck)

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

from java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructors

public Builder(int focusGain)

Constructs a new Builder, and specifies how audio focus will be requested. Valid values for focus requests are AudioManagerCompat.AUDIOFOCUS_GAIN, AudioManagerCompat.AUDIOFOCUS_GAIN_TRANSIENT, and AudioManagerCompat.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK, and AudioManagerCompat.AUDIOFOCUS_GAIN_TRANSIENT_EXCLUSIVE. AudioManagerCompat.AUDIOFOCUS_GAIN_TRANSIENT_EXCLUSIVE is converted to AudioManagerCompat.AUDIOFOCUS_GAIN_TRANSIENT on API levels previous to API 19.

By default there is no focus change listener, delayed focus is not supported, ducking is suitable for the application, and the AudioAttributesCompat have a usage of AudioAttributes.

Parameters:

focusGain: the type of audio focus gain that will be requested

public Builder(AudioFocusRequestCompat requestToCopy)

Constructs a new Builder with all the properties of the AudioFocusRequestCompat passed as parameter. Use this method when you want a new request to differ only by some properties.

Parameters:

requestToCopy: the non-null AudioFocusRequestCompat to duplicate.

Methods

public AudioFocusRequestCompat.Builder setFocusGain(int focusGain)

Sets the type of focus gain that will be requested. Use this method to replace the focus gain when building a request by modifying an existing AudioFocusRequestCompat instance.

Parameters:

focusGain: the type of audio focus gain that will be requested.

Returns:

this Builder instance

public AudioFocusRequestCompat.Builder setOnAudioFocusChangeListener(OnAudioFocusChangeListener listener)

Sets the listener called when audio focus changes after being requested with AudioManagerCompat.requestAudioFocus(AudioManager, AudioFocusRequestCompat), and until being abandoned with AudioManagerCompat.abandonAudioFocusRequest(AudioManager, AudioFocusRequestCompat). 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: the listener receiving the focus change notifications.

Returns:

this Builder instance.

public AudioFocusRequestCompat.Builder setOnAudioFocusChangeListener(OnAudioFocusChangeListener listener, Handler handler)

Sets the listener called when audio focus changes after being requested with AudioManagerCompat.requestAudioFocus(AudioManager, AudioFocusRequestCompat), and until being abandoned with AudioManagerCompat.abandonAudioFocusRequest(AudioManager, AudioFocusRequestCompat). 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: the listener receiving the focus change notifications.
handler: the Handler for the thread on which to execute the notifications.

Returns:

this Builder instance.

public AudioFocusRequestCompat.Builder setAudioAttributes(AudioAttributesCompat attributes)

Sets the AudioAttributesCompat 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 AudioAttributesCompat as used for playback, see for example MediaPlayer in MediaPlayer or in AudioTrack.

Parameters:

attributes: the AudioAttributesCompat for the focus request.

Returns:

this Builder instance.

public AudioFocusRequestCompat.Builder setWillPauseWhenDucked(boolean pauseOnDuck)

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

Parameters:

pauseOnDuck: use true if the application intends to pause audio playback when losing focus with AudioManager.

Returns:

this Builder instance.

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

Returns:

the AudioFocusRequestCompat instance qualified by all the properties set on this Builder.