Skip to content

Most visited

Recently visited

navigation

AudioTrack.Builder

public static class AudioTrack.Builder
extends Object

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


Builder class for AudioTrack objects. Use this class to configure and create an AudioTrack instance. By setting audio attributes and audio format parameters, you indicate which of those vary from the default behavior on the device.

Here is an example where Builder is used to specify all AudioFormat parameters, to be used by a new AudioTrack instance:

 AudioTrack player = new AudioTrack.Builder()
         .setAudioAttributes(new AudioAttributes.Builder()
                  .setUsage(AudioAttributes.USAGE_ALARM)
                  .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
                  .build())
         .setAudioFormat(new AudioFormat.Builder()
                 .setEncoding(AudioFormat.ENCODING_PCM_16BIT)
                 .setSampleRate(44100)
                 .setChannelMask(AudioFormat.CHANNEL_OUT_STEREO)
                 .build())
         .setBufferSizeInBytes(minBuffSize)
         .build();
 

If the audio attributes are not set with setAudioAttributes(AudioAttributes), attributes comprising USAGE_MEDIA will be used.
If the audio format is not specified or is incomplete, its channel configuration will be CHANNEL_OUT_STEREO and the encoding will be ENCODING_PCM_16BIT. The sample rate will depend on the device actually selected for playback and can be queried with getSampleRate() method.
If the buffer size is not specified with setBufferSizeInBytes(int), and the mode is MODE_STREAM, the minimum buffer size is used.
If the transfer mode is not specified with setTransferMode(int), MODE_STREAM will be used.
If the session ID is not specified with setSessionId(int), a new one will be generated.

Summary

Public constructors

AudioTrack.Builder()

Constructs a new Builder with the default values as described above.

Public methods

AudioTrack build()

Builds an AudioTrack instance initialized with all the parameters set on this Builder.

AudioTrack.Builder setAudioAttributes(AudioAttributes attributes)

Sets the AudioAttributes.

AudioTrack.Builder setAudioFormat(AudioFormat format)

Sets the format of the audio data to be played by the AudioTrack.

AudioTrack.Builder setBufferSizeInBytes(int bufferSizeInBytes)

Sets the total size (in bytes) of the buffer where audio data is read from for playback.

AudioTrack.Builder setPerformanceMode(int performanceMode)

Sets the AudioTrack performance mode.

AudioTrack.Builder setSessionId(int sessionId)

Sets the session ID the AudioTrack will be attached to.

AudioTrack.Builder setTransferMode(int mode)

Sets the mode under which buffers of audio data are transferred from the AudioTrack to the framework.

Inherited methods

From class java.lang.Object

Public constructors

AudioTrack.Builder

added in API level 23
AudioTrack.Builder ()

Constructs a new Builder with the default values as described above.

Public methods

build

added in API level 23
AudioTrack build ()

Builds an AudioTrack instance initialized with all the parameters set on this Builder.

Returns
AudioTrack a new successfully initialized AudioTrack instance.

This value will never be null.

Throws
UnsupportedOperationException if the parameters set on the Builder were incompatible, or if they are not supported by the device, or if the device was not available.

setAudioAttributes

added in API level 23
AudioTrack.Builder setAudioAttributes (AudioAttributes attributes)

Sets the AudioAttributes.

Parameters
attributes AudioAttributes: a non-null AudioAttributes instance that describes the audio data to be played.

Returns
AudioTrack.Builder the same Builder instance.

This value will never be null.

Throws
IllegalArgumentException

setAudioFormat

added in API level 23
AudioTrack.Builder setAudioFormat (AudioFormat format)

Sets the format of the audio data to be played by the AudioTrack. See AudioFormat.Builder for configuring the audio format parameters such as encoding, channel mask and sample rate.

Parameters
format AudioFormat: a non-null AudioFormat instance.

Returns
AudioTrack.Builder the same Builder instance.

This value will never be null.

Throws
IllegalArgumentException

setBufferSizeInBytes

added in API level 23
AudioTrack.Builder setBufferSizeInBytes (int bufferSizeInBytes)

Sets the total size (in bytes) of the buffer where audio data is read from for playback. If using the AudioTrack in streaming mode (see MODE_STREAM, you can write data into this buffer in smaller chunks than this size. See getMinBufferSize(int, int, int) to determine the estimated minimum buffer size for the creation of an AudioTrack instance in streaming mode.
If using the AudioTrack in static mode (see MODE_STATIC), this is the maximum size of the sound that will be played by this instance.

Returns
AudioTrack.Builder the same Builder instance.

This value will never be null.

Throws
IllegalArgumentException

setPerformanceMode

added in API level 26
AudioTrack.Builder setPerformanceMode (int performanceMode)

Sets the AudioTrack performance mode. This is an advisory request which may not be supported by the particular device, and the framework is free to ignore such request if it is incompatible with other requests or hardware.

Parameters
performanceMode int: one of PERFORMANCE_MODE_NONE, PERFORMANCE_MODE_LOW_LATENCY, or PERFORMANCE_MODE_POWER_SAVING.

Returns
AudioTrack.Builder the same Builder instance.

This value will never be null.

Throws
IllegalArgumentException if performanceMode is not valid.

setSessionId

added in API level 23
AudioTrack.Builder setSessionId (int sessionId)

Sets the session ID the AudioTrack will be attached to.

Parameters
sessionId int: a strictly positive ID number retrieved from another AudioTrack via getAudioSessionId() or allocated by AudioManager via generateAudioSessionId(), or AUDIO_SESSION_ID_GENERATE.

Returns
AudioTrack.Builder the same Builder instance.

This value will never be null.

Throws
IllegalArgumentException

setTransferMode

added in API level 23
AudioTrack.Builder setTransferMode (int mode)

Sets the mode under which buffers of audio data are transferred from the AudioTrack to the framework.

Parameters
mode int: one of MODE_STREAM, MODE_STATIC.

Returns
AudioTrack.Builder the same Builder instance.

This value will never be null.

Throws
IllegalArgumentException
This site uses cookies to store your preferences for site-specific language and display options.

Get the latest Android developer news and tips that will help you find success on Google Play.

* Required Fields

Hooray!

Browse this site in ?

You requested a page in , but your language preference for this site is .

Would you like to change your language preference and browse this site in ? If you want to change your language preference later, use the language menu at the bottom of each page.

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.

Take a short survey?
Help us improve the Android developer experience.
(Sep 2017 survey)