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(CONTENT_TYPE_MUSIC) .build()) .setAudioFormat(new AudioFormat.Builder() .setEncoding(AudioFormat.ENCODING_PCM_16BIT) .setSampleRate(441000) .setChannelMask(AudioFormat.CHANNEL_OUT_STEREO) .build()) .setBufferSize(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 sample rate will be the
default output sample rate of the device (see
PROPERTY_OUTPUT_SAMPLE_RATE
), its channel configuration will be
CHANNEL_OUT_STEREO
and the encoding will be
ENCODING_PCM_16BIT
.
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.
Public Constructors | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Constructs a new Builder with the default values as described above.
|
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Builds an
AudioTrack instance initialized with all the parameters set
on this Builder .
| |||||||||||
Sets the
AudioAttributes .
| |||||||||||
Sets the format of the audio data to be played by the
AudioTrack .
| |||||||||||
Sets the total size (in bytes) of the buffer where audio data is read from for playback.
| |||||||||||
Sets the session ID the
AudioTrack will be attached to.
| |||||||||||
Sets the mode under which buffers of audio data are transferred from the
AudioTrack to the framework.
|
[Expand]
Inherited Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
From class
java.lang.Object
|
Constructs a new Builder with the default values as described above.
Builds an AudioTrack
instance initialized with all the parameters set
on this Builder
.
AudioTrack
instance.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.
|
---|
Sets the AudioAttributes
.
attributes | a non-null AudioAttributes instance that describes the audio
data to be played. |
---|
IllegalArgumentException |
---|
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.
format | a non-null AudioFormat instance. |
---|
IllegalArgumentException |
---|
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 minimum required buffer size for the successful creation of an AudioTrack instance
in streaming mode. Using values smaller than getMinBufferSize()
will result
in an exception when trying to build the AudioTrack
.
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.
IllegalArgumentException |
---|
Sets the session ID the AudioTrack
will be attached to.
sessionId | a strictly positive ID number retrieved from another
AudioTrack via getAudioSessionId() or allocated by
AudioManager via generateAudioSessionId() , or
AUDIO_SESSION_ID_GENERATE . |
---|
IllegalArgumentException |
---|
Sets the mode under which buffers of audio data are transferred from the
AudioTrack
to the framework.
mode | one of MODE_STREAM , MODE_STATIC . |
---|
IllegalArgumentException |
---|