AudioTrack.Builder
public
static
class
AudioTrack.Builder
extends Object
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(android.media.AudioAttributes)
,
attributes comprising AudioAttributes#USAGE_MEDIA
will be used.
If the audio format is not specified or is incomplete, its channel configuration will be
AudioFormat#CHANNEL_OUT_STEREO
and the encoding will be
AudioFormat#ENCODING_PCM_16BIT
.
The sample rate will depend on the device actually selected for playback and can be queried
with AudioTrack.getSampleRate()
method.
If the buffer size is not specified with setBufferSizeInBytes(int)
,
and the mode is AudioTrack#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.
Offload is false by default.
Summary
Public constructors |
Builder()
Constructs a new Builder with the default values as described above.
|
Inherited methods |
From class
java.lang.Object
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 timeout, int nanos)
Causes the current thread to wait until another thread invokes the
notify() method or the
notifyAll() method for this object, or
some other thread interrupts the current thread, or a certain
amount of real time has elapsed.
|
final
void
|
wait(long timeout)
Causes the current thread to wait until either another thread invokes the
notify() method or the
notifyAll() method for this object, or a
specified amount of time has elapsed.
|
final
void
|
wait()
Causes the current thread to wait until another thread invokes the
notify() method or the
notifyAll() method for this object.
|
|
Public constructors
Builder
public Builder ()
Constructs a new Builder with the default values as described above.
Public methods
build
public AudioTrack build ()
Builds an AudioTrack
instance initialized with all the parameters set
on this Builder
.
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. |
setBufferSizeInBytes
public 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 AudioTrack#MODE_STREAM
, you can write data into this buffer in smaller
chunks than this size. See AudioTrack.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
AudioTrack#MODE_STATIC
), this is the maximum size of the sound that will be
played by this instance.
Parameters |
bufferSizeInBytes |
int : Value is 0 or greater |
setEncapsulationMode
public AudioTrack.Builder setEncapsulationMode (int encapsulationMode)
Sets the encapsulation mode.
Encapsulation mode allows metadata to be sent together with
the audio data payload in a ByteBuffer
.
This requires a compatible hardware audio codec.
setOffloadedPlayback
public AudioTrack.Builder setOffloadedPlayback (boolean offload)
Sets whether this track will play through the offloaded audio path.
When set to true, at build time, the audio format will be checked against
AudioManager#isOffloadedPlaybackSupported(AudioFormat,AudioAttributes)
to verify the audio format used by this track is supported on the device's offload
path (if any).
Offload is only supported for media audio streams, and therefore requires that
the usage be AudioAttributes#USAGE_MEDIA
.
Parameters |
offload |
boolean : true to require the offload path for playback. |
public 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.
setTransferMode
public AudioTrack.Builder setTransferMode (int mode)
Sets the mode under which buffers of audio data are transferred from the
AudioTrack
to the framework.