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

Object clone()

Creates and returns a copy of this object.

boolean equals(Object obj)

Indicates whether some other object is "equal to" this one.

void finalize()

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.

final Class<?> getClass()

Returns the runtime class of this Object.

int hashCode()

Returns a hash code value for the object.

final void notify()

Wakes up a single thread that is waiting on this object's monitor.

final void notifyAll()

Wakes up all threads that are waiting on this object's monitor.

String toString()

Returns a string representation of the object.

final void wait(long timeoutMillis, int nanos)

Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed.

final void wait(long timeoutMillis)

Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed.

final void wait()

Causes the current thread to wait until it is awakened, typically by being notified or interrupted.

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.

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.

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.

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.

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.

Returns
AudioAttributes.Builder the same Builder instance.