Passer directement au contenu

Les plus consultés

Consultés récemment

navigation

AudioFocusRequest.Builder

public static final class AudioFocusRequest.Builder
extends Object

java.lang.Object
   ↳ android.media.AudioFocusRequest.Builder


Builder class for AudioFocusRequest objects.

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

focus listener and handlernone
AudioAttributesattributes with usage set to USAGE_MEDIA
pauses on duckfalse
supports delayed focus grantfalse

Summary

Public constructors

AudioFocusRequest.Builder(int focusGain)

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

AudioFocusRequest.Builder(AudioFocusRequest requestToCopy)

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

Public methods

AudioFocusRequest build()

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

AudioFocusRequest.Builder setAcceptsDelayedFocusGain(boolean acceptsDelayedFocusGain)

Marks this focus request as compatible with delayed focus.

AudioFocusRequest.Builder setAudioAttributes(AudioAttributes attributes)

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

AudioFocusRequest.Builder setFocusGain(int focusGain)

Sets the type of focus gain that will be requested.

AudioFocusRequest.Builder setForceDucking(boolean forceDucking)

Marks this focus request as forcing ducking, regardless of the conditions in which the system would or would not enforce ducking.

AudioFocusRequest.Builder setOnAudioFocusChangeListener(AudioManager.OnAudioFocusChangeListener listener)

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

AudioFocusRequest.Builder setOnAudioFocusChangeListener(AudioManager.OnAudioFocusChangeListener listener, Handler handler)

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

AudioFocusRequest.Builder setWillPauseWhenDucked(boolean pauseOnDuck)

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

Inherited methods

Public constructors

AudioFocusRequest.Builder

added in API level 26
AudioFocusRequest.Builder (int focusGain)

Constructs a new Builder, and specifies how audio focus will be requested. Valid values for focus requests are AUDIOFOCUS_GAIN, AUDIOFOCUS_GAIN_TRANSIENT, AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK, and AUDIOFOCUS_GAIN_TRANSIENT_EXCLUSIVE.

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

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

Throws
IllegalArgumentException thrown when an invalid focus gain type is used

AudioFocusRequest.Builder

added in API level 26
AudioFocusRequest.Builder (AudioFocusRequest requestToCopy)

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

Parameters
requestToCopy AudioFocusRequest: the non-null AudioFocusRequest to build a duplicate from.

Throws
IllegalArgumentException thrown when a null AudioFocusRequest is used.

Public methods

build

added in API level 26
AudioFocusRequest build ()

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

Returns
AudioFocusRequest the AudioFocusRequest instance qualified by all the properties set on this Builder.

Throws
IllegalStateException thrown when attempting to build a focus request that is set to accept delayed focus, or to pause on duck, but no focus change listener was set.

setAcceptsDelayedFocusGain

added in API level 26
AudioFocusRequest.Builder setAcceptsDelayedFocusGain (boolean acceptsDelayedFocusGain)

Marks this focus request as compatible with delayed focus. See more details about delayed focus in the AudioFocusRequest class documentation.

Parameters
acceptsDelayedFocusGain boolean: use true if the application supports delayed focus. If true, note that you must also set a focus listener to be notified of delayed focus gain, with setOnAudioFocusChangeListener(OnAudioFocusChangeListener, Handler).

Returns
AudioFocusRequest.Builder this Builder instance

This value will never be null.

setAudioAttributes

added in API level 26
AudioFocusRequest.Builder setAudioAttributes (AudioAttributes attributes)

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 setAudioAttributes(AudioAttributes) in MediaPlayer or setAudioAttributes(AudioAttributes) in AudioTrack.

Parameters
attributes AudioAttributes: the AudioAttributes for the focus request.

This value must never be null.

Returns
AudioFocusRequest.Builder this Builder instance.

This value will never be null.

Throws
NullPointerException thrown when using null for the attributes.

setFocusGain

added in API level 26
AudioFocusRequest.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 AudioFocusRequest instance.

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

Returns
AudioFocusRequest.Builder this Builder instance

This value will never be null.

Throws
IllegalArgumentException thrown when an invalid focus gain type is used

setForceDucking

AudioFocusRequest.Builder setForceDucking (boolean forceDucking)

Marks this focus request as forcing ducking, regardless of the conditions in which the system would or would not enforce ducking. Forcing ducking will only be honored when requesting AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK with an AudioAttributes usage of USAGE_ASSISTANCE_ACCESSIBILITY, coming from an accessibility service, and will be ignored otherwise.

Parameters
forceDucking boolean: true to force ducking

Returns
AudioFocusRequest.Builder this Builder instance

This value will never be null.

setOnAudioFocusChangeListener

added in API level 26
AudioFocusRequest.Builder setOnAudioFocusChangeListener (AudioManager.OnAudioFocusChangeListener listener)

Sets the listener called when audio focus changes after being requested with requestAudioFocus(AudioFocusRequest), and until being abandoned with abandonAudioFocusRequest(AudioFocusRequest). 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 Looper.

Parameters
listener AudioManager.OnAudioFocusChangeListener: the listener receiving the focus change notifications.

This value must never be null.

Returns
AudioFocusRequest.Builder this Builder instance.

This value will never be null.

Throws
NullPointerException thrown when a null focus listener is used.

setOnAudioFocusChangeListener

added in API level 26
AudioFocusRequest.Builder setOnAudioFocusChangeListener (AudioManager.OnAudioFocusChangeListener listener, 
                Handler handler)

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

This value must never be null.

handler Handler: the Handler for the thread on which to execute the notifications.

This value must never be null.

Returns
AudioFocusRequest.Builder this Builder instance.

This value will never be null.

Throws
NullPointerException thrown when a null focus listener or handler is used.

setWillPauseWhenDucked

added in API level 26
AudioFocusRequest.Builder setWillPauseWhenDucked (boolean pauseOnDuck)

Declare the intended behavior of the application with regards to audio ducking. See more details in the AudioFocusRequest class documentation.

Parameters
pauseOnDuck boolean: use true if the application intends to pause audio playback when losing focus with AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK. If true, note that you must also set a focus listener to receive such an event, with setOnAudioFocusChangeListener(OnAudioFocusChangeListener, Handler).

Returns
AudioFocusRequest.Builder this Builder instance.

This value will never be null.

Ce site Web utilise des cookies pour enregistrer vos préférences relatives à la langue du site et aux options d'affichage.

Recevez les dernières actualités destinées aux développeurs Android, ainsi que des conseils qui vous mèneront vers le succès sur Google Play.

* Champs obligatoires

Super !

Suivez Google Developers sur WeChat

Consulter ce site en  ?

Vous avez demandé une page en , mais la langue indiquée dans vos préférences linguistiques pour ce site est la suivante : .

Souhaitez-vous modifier vos préférences linguistiques et parcourir ce site en  ? Pour modifier ultérieurement vos préférences linguistiques, utilisez le menu "Langue" au bas de chaque page.

Cette classe nécessite le niveau d'API  ou supérieur

Ce document est masqué, car vous avez sélectionné le niveau d'API  pour la documentation. Vous pouvez modifier le niveau d'API pour la documentation avec le sélecteur situé au-dessus du menu de navigation de gauche.

Pour découvrir comment déterminer le niveau d'API nécessaire pour votre application, consultez le guide sur la prise en charge des différentes versions de plate-forme (en anglais).

Take a short survey?
Help us improve the Android developer experience. (April 2018 — Developer Survey)