Added in API level 21

Builder


class Builder
kotlin.Any
   ↳ 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

Constructs a new Builder with the defaults.

Constructs a new Builder from a given AudioAttributes

Public methods
open AudioAttributes!

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

open AudioAttributes.Builder
setAllowedCapturePolicy(capturePolicy: Int)

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

open AudioAttributes.Builder!
setContentType(contentType: Int)

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

open AudioAttributes.Builder!
setFlags(flags: Int)

Sets the combination of flags.

open AudioAttributes.Builder

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

open AudioAttributes.Builder

Specifies whether the content has already been processed for spatialization.

open AudioAttributes.Builder!
setLegacyStreamType(streamType: Int)

Sets attributes as inferred from the legacy stream types.

open AudioAttributes.Builder

Sets the behavior affecting whether spatialization will be used.

open AudioAttributes.Builder!
setUsage(usage: Int)

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

Public constructors

Builder

Added in API level 21
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
Builder(aa: AudioAttributes!)

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
open fun build(): AudioAttributes!

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

Return
AudioAttributes! a new AudioAttributes object

setAllowedCapturePolicy

Added in API level 29
open fun setAllowedCapturePolicy(capturePolicy: Int): AudioAttributes.Builder

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 android.media.AudioAttributes#ALLOW_CAPTURE_BY_ALL, android.media.AudioAttributes#ALLOW_CAPTURE_BY_SYSTEM, or android.media.AudioAttributes#ALLOW_CAPTURE_BY_NONE
Return
AudioAttributes.Builder the same Builder instance This value cannot be null.
Exceptions
java.lang.IllegalArgumentException if the argument is not a valid value.

setFlags

Added in API level 21
open fun setFlags(flags: Int): AudioAttributes.Builder!

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.
Return
AudioAttributes.Builder! the same Builder instance.

setHapticChannelsMuted

Added in API level 29
open fun setHapticChannelsMuted(muted: Boolean): AudioAttributes.Builder

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 android.Manifest.permission#VIBRATE permission.

Parameters
muted Boolean: true to force muting haptic channels.
Return
AudioAttributes.Builder the same Builder instance. This value cannot be null.

setIsContentSpatialized

Added in API level 32
open fun setIsContentSpatialized(isSpatialized: Boolean): AudioAttributes.Builder

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

Parameters
isSpatialized Boolean:
Return
AudioAttributes.Builder the same Builder instance This value cannot be null.

setLegacyStreamType

Added in API level 21
open fun setLegacyStreamType(streamType: Int): AudioAttributes.Builder!

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.
Return
AudioAttributes.Builder! the same Builder instance.

setSpatializationBehavior

Added in API level 32
open fun setSpatializationBehavior(sb: Int): AudioAttributes.Builder

Sets the behavior affecting whether spatialization will be used.

Parameters
sb Int: the spatialization behavior Value is android.media.AudioAttributes#SPATIALIZATION_BEHAVIOR_AUTO, or android.media.AudioAttributes#SPATIALIZATION_BEHAVIOR_NEVER
Return
AudioAttributes.Builder the same Builder instance This value cannot be null.