AudioAttributes.Builder


public static class AudioAttributes.Builder
extends Object

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


Builder class for AudioAttributes objects.

Here is an example where Builder is used to define the AudioAttributes to be used by a new AudioTrack instance:

 AudioTrack myTrack = new AudioTrack(
         new AudioAttributes.Builder()
             .setUsage(AudioAttributes.USAGE_MEDIA)
             .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
             .build(),
         myFormat, myBuffSize, AudioTrack.MODE_STREAM, mySession);
 

By default all types of information (usage, content type, flags) conveyed by an AudioAttributes instance are set to "unknown". Unknown information will be interpreted as a default value that is dependent on the context of use, for instance a MediaPlayer will use a default usage of AudioAttributes#USAGE_MEDIA.

Summary

Public constructors

Builder()

Constructs a new Builder with the defaults.

Builder(AudioAttributes aa)

Constructs a new Builder from a given AudioAttributes

Public methods

AudioAttributes build()

Combines all of the attributes that have been set and return a new AudioAttributes object.

AudioAttributes.Builder setAllowedCapturePolicy(int capturePolicy)

Specifies whether the audio may or may not be captured by other apps or the system.

AudioAttributes.Builder setContentType(int contentType)

Sets the attribute describing the content type of the audio signal, such as speech, or music.

AudioAttributes.Builder setFlags(int flags)

Sets the combination of flags.

AudioAttributes.Builder setHapticChannelsMuted(boolean muted)

Specifying if haptic should be muted or not when playing audio-haptic coupled data.

AudioAttributes.Builder setIsContentSpatialized(boolean isSpatialized)

Specifies whether the content has already been processed for spatialization.

AudioAttributes.Builder setLegacyStreamType(int streamType)

Sets attributes as inferred from the legacy stream types.

AudioAttributes.Builder setSpatializationBehavior(int sb)

Sets the behavior affecting whether spatialization will be used.

AudioAttributes.Builder setUsage(int usage)

Sets the attribute describing what is the intended use of the audio signal, such as alarm or ringtone.

Inherited methods

Public constructors

Builder

Added in API level 21
public Builder ()

Constructs a new Builder with the defaults. By default, usage and content type are respectively AudioAttributes#USAGE_UNKNOWN and AudioAttributes#CONTENT_TYPE_UNKNOWN, and flags are 0. It is recommended to configure the usage (with setUsage(int)) or deriving attributes from a legacy stream type (with setLegacyStreamType(int)) before calling build() to override any default playback behavior in terms of routing and volume management.

Builder

Added in API level 21
public Builder (AudioAttributes aa)

Constructs a new Builder from a given AudioAttributes

Parameters
aa AudioAttributes: the AudioAttributes object whose data will be reused in the new Builder.

Public methods

build

Added in API level 21
public AudioAttributes build ()

Combines all of the attributes that have been set and return a new AudioAttributes object.

Returns
AudioAttributes a new AudioAttributes object

setAllowedCapturePolicy

Added in API level 29
public AudioAttributes.Builder setAllowedCapturePolicy (int capturePolicy)

Specifies whether the audio may or may not be captured by other apps or the system. The default is AudioAttributes#ALLOW_CAPTURE_BY_ALL. There are multiple ways to set this policy:

  • for each track independently, with this method
  • application-wide at runtime, with AudioManager#setAllowedCapturePolicy(int)
  • application-wide at build time, see allowAudioPlaybackCapture in the application manifest.
The most restrictive policy is always applied. See AudioPlaybackCaptureConfiguration for more details on which audio signals can be captured.

Parameters
capturePolicy int: Value is AudioAttributes.ALLOW_CAPTURE_BY_ALL, AudioAttributes.ALLOW_CAPTURE_BY_SYSTEM, or AudioAttributes.ALLOW_CAPTURE_BY_NONE

Returns
AudioAttributes.Builder the same Builder instance This value cannot be null.

Throws
IllegalArgumentException if the argument is not a valid value.

setContentType

Added in API level 21
public AudioAttributes.Builder setContentType (int contentType)

Sets the attribute describing the content type of the audio signal, such as speech, or music.

Parameters
contentType int: the content type values, one of AudioAttributes#CONTENT_TYPE_MOVIE, AudioAttributes#CONTENT_TYPE_MUSIC, AudioAttributes#CONTENT_TYPE_SONIFICATION, AudioAttributes#CONTENT_TYPE_SPEECH, AudioAttributes#CONTENT_TYPE_UNKNOWN. Value is AudioAttributes.CONTENT_TYPE_UNKNOWN, AudioAttributes.CONTENT_TYPE_SPEECH, AudioAttributes.CONTENT_TYPE_MUSIC, AudioAttributes.CONTENT_TYPE_MOVIE, or AudioAttributes.CONTENT_TYPE_SONIFICATION

Returns
AudioAttributes.Builder the same Builder instance.

setFlags

Added in API level 21
public AudioAttributes.Builder setFlags (int flags)

Sets the combination of flags. This is a bitwise OR with the existing flags.

Parameters
flags int: a combination of AudioAttributes#FLAG_AUDIBILITY_ENFORCED, AudioAttributes#FLAG_HW_AV_SYNC.

Returns
AudioAttributes.Builder the same Builder instance.

setHapticChannelsMuted

Added in API level 29
public AudioAttributes.Builder setHapticChannelsMuted (boolean muted)

Specifying if haptic should be muted or not when playing audio-haptic coupled data. By default, haptic channels are disabled.

This will be ignored if the caller doesn't have the Manifest.permission.VIBRATE permission.

Parameters
muted boolean: true to force muting haptic channels.

Returns
AudioAttributes.Builder the same Builder instance. This value cannot be null.

setIsContentSpatialized

Added in API level 32
public AudioAttributes.Builder setIsContentSpatialized (boolean isSpatialized)

Specifies whether the content has already been processed for spatialization. If it has, setting this to true will prevent issues such as double-processing.

Returns
AudioAttributes.Builder the same Builder instance This value cannot be null.

setLegacyStreamType

Added in API level 21
public AudioAttributes.Builder setLegacyStreamType (int streamType)

Sets attributes as inferred from the legacy stream types. Warning: do not use this method in combination with setting any other attributes such as usage, content type, flags or haptic control, as this method will overwrite (the more accurate) information describing the use case previously set in the Builder. In general, avoid using it and prefer setting usage and content type directly with setUsage(int) and setContentType(int).

Use this method when building an AudioAttributes instance to initialize some of the attributes by information derived from a legacy stream type.

Parameters
streamType int: one of AudioManager#STREAM_VOICE_CALL, AudioManager#STREAM_SYSTEM, AudioManager#STREAM_RING, AudioManager#STREAM_MUSIC, AudioManager#STREAM_ALARM, or AudioManager#STREAM_NOTIFICATION.

Returns
AudioAttributes.Builder the same Builder instance.

setSpatializationBehavior

Added in API level 32
public AudioAttributes.Builder setSpatializationBehavior (int sb)

Sets the behavior affecting whether spatialization will be used.

Parameters
sb int: the spatialization behavior Value is AudioAttributes.SPATIALIZATION_BEHAVIOR_AUTO, or AudioAttributes.SPATIALIZATION_BEHAVIOR_NEVER

Returns
AudioAttributes.Builder the same Builder instance This value cannot be null.

setUsage

Added in API level 21
public AudioAttributes.Builder setUsage (int usage)

Sets the attribute describing what is the intended use of the audio signal, such as alarm or ringtone.

Parameters
usage int: the usage to set. Value is AudioAttributes.USAGE_UNKNOWN, AudioAttributes.USAGE_MEDIA, AudioAttributes.USAGE_VOICE_COMMUNICATION, AudioAttributes.USAGE_VOICE_COMMUNICATION_SIGNALLING, AudioAttributes.USAGE_ALARM, AudioAttributes.USAGE_NOTIFICATION, AudioAttributes.USAGE_NOTIFICATION_RINGTONE, AudioAttributes.USAGE_NOTIFICATION_COMMUNICATION_REQUEST, AudioAttributes.USAGE_NOTIFICATION_COMMUNICATION_INSTANT, AudioAttributes.USAGE_NOTIFICATION_COMMUNICATION_DELAYED, AudioAttributes.USAGE_NOTIFICATION_EVENT, AudioAttributes.USAGE_ASSISTANCE_ACCESSIBILITY, AudioAttributes.USAGE_ASSISTANCE_NAVIGATION_GUIDANCE, AudioAttributes.USAGE_ASSISTANCE_SONIFICATION, AudioAttributes.USAGE_GAME, or AudioAttributes.USAGE_ASSISTANT

Returns
AudioAttributes.Builder the same Builder instance.