java.lang.Object | |
↳ | android.media.AudioTrack |
The AudioTrack class manages and plays a single audio resource for Java applications. It allows to stream PCM audio buffers to the audio hardware for playback. This is achieved by "pushing" the data to the AudioTrack object using one of the write(byte[], int, int) and write(short[], int, int) methods.
An AudioTrack instance can operate under two modes: static or streaming.
In Streaming mode, the application writes a continuous stream of data to the AudioTrack, using
one of the write() methods. These are blocking and return when the data has been transferred
from the Java layer to the native layer and queued for playback. The streaming mode
is most useful when playing blocks of audio data that for instance are:
Upon creation, an AudioTrack object initializes its associated audio buffer.
The size of this buffer, specified during the construction, determines how long an AudioTrack
can play before running out of data.
For an AudioTrack using the static mode, this size is the maximum size of the sound that can
be played from it.
For the streaming mode, data will be written to the hardware in chunks of
sizes inferior to the total buffer size.
Nested Classes | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
AudioTrack.OnPlaybackPositionUpdateListener | Interface definition for a callback to be invoked when the playback head position of an AudioTrack has reached a notification marker or has increased by a certain period. |
Constants | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
int | ERROR | Denotes a generic operation failure. | |||||||||
int | ERROR_BAD_VALUE | Denotes a failure due to the use of an invalid value. | |||||||||
int | ERROR_INVALID_OPERATION | Denotes a failure due to the improper use of a method. | |||||||||
int | MODE_STATIC | Creation mode where audio data is transferred from Java to the native layer only once before the audio starts playing. | |||||||||
int | MODE_STREAM | Creation mode where audio data is streamed from Java to the native layer as the audio is playing. | |||||||||
int | PLAYSTATE_PAUSED | indicates AudioTrack state is paused | |||||||||
int | PLAYSTATE_PLAYING | indicates AudioTrack state is playing | |||||||||
int | PLAYSTATE_STOPPED | indicates AudioTrack state is stopped | |||||||||
int | STATE_INITIALIZED | State of an AudioTrack that is ready to be used. | |||||||||
int | STATE_NO_STATIC_DATA | State of a successfully initialized AudioTrack that uses static data, but that hasn't received that data yet. | |||||||||
int | STATE_UNINITIALIZED | State of an AudioTrack that was not successfully initialized upon creation. | |||||||||
int | SUCCESS | Denotes a successful operation. |
Public Constructors | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Class constructor.
|
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Flushes the audio data currently queued for playback.
| |||||||||||
Returns the configured audio data format.
| |||||||||||
Returns the configured channel configuration.
| |||||||||||
Returns the configured number of channels.
| |||||||||||
Returns the maximum valid volume value.
| |||||||||||
Returns the minimum buffer size required for the successful creation of an AudioTrack
object to be created in the MODE_STREAM mode.
| |||||||||||
Returns the minimum valid volume value.
| |||||||||||
Returns the hardware output sample rate
| |||||||||||
Returns marker position expressed in frames.
| |||||||||||
Returns the playback state of the AudioTrack instance.
| |||||||||||
Returns the playback head position expressed in frames
| |||||||||||
Returns the current playback rate in Hz.
| |||||||||||
Returns the notification update period expressed in frames.
| |||||||||||
Returns the configured audio data sample rate in Hz
| |||||||||||
Returns the state of the AudioTrack instance.
| |||||||||||
Returns the type of audio stream this AudioTrack is configured for.
| |||||||||||
Pauses the playback of the audio data.
| |||||||||||
Starts playing an AudioTrack.
| |||||||||||
Releases the native AudioTrack resources.
| |||||||||||
Notifies the native resource to reuse the audio data already loaded in the native
layer.
| |||||||||||
Sets the loop points and the loop count.
| |||||||||||
Sets the position of the notification marker.
| |||||||||||
Sets the playback head position.
| |||||||||||
Sets the listener the AudioTrack notifies when a previously set marker is reached or
for each periodic playback head position update.
| |||||||||||
Sets the listener the AudioTrack notifies when a previously set marker is reached or
for each periodic playback head position update.
| |||||||||||
Sets the playback sample rate for this track.
| |||||||||||
Sets the period for the periodic notification event.
| |||||||||||
Sets the specified left/right output volume values on the AudioTrack.
| |||||||||||
Stops playing the audio data.
| |||||||||||
Writes the audio data to the audio hardware for playback.
| |||||||||||
Writes the audio data to the audio hardware for playback.
|
Protected Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Is called before the object's memory is being reclaimed by the VM.
| |||||||||||
Returns the native frame count used by the hardware.
| |||||||||||
Sets the initialization state of the instance.
|
[Expand]
Inherited Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
From class java.lang.Object
|
Denotes a generic operation failure.
Denotes a failure due to the use of an invalid value.
Denotes a failure due to the improper use of a method.
Creation mode where audio data is transferred from Java to the native layer only once before the audio starts playing.
Creation mode where audio data is streamed from Java to the native layer as the audio is playing.
indicates AudioTrack state is paused
indicates AudioTrack state is playing
indicates AudioTrack state is stopped
State of an AudioTrack that is ready to be used.
State of a successfully initialized AudioTrack that uses static data, but that hasn't received that data yet.
State of an AudioTrack that was not successfully initialized upon creation.
Denotes a successful operation.
Class constructor.
streamType | the type of the audio stream. See STREAM_VOICE_CALL, STREAM_SYSTEM, STREAM_RING, STREAM_MUSIC and STREAM_ALARM |
---|---|
sampleRateInHz | the sample rate expressed in Hertz. Examples of rates are (but not limited to) 44100, 22050 and 11025. |
channelConfig | describes the configuration of the audio channels. See CHANNEL_CONFIGURATION_MONO and CHANNEL_CONFIGURATION_STEREO |
audioFormat | the format in which the audio data is represented. See ENCODING_PCM_16BIT and ENCODING_PCM_8BIT |
bufferSizeInBytes | the total size (in bytes) of the buffer where audio data is read from for playback. If using the AudioTrack in streaming mode, you can write data into this buffer in smaller chunks than this size. If using the AudioTrack in static mode, this is the maximum size of the sound that will be played for this instance. 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 initialization failure. |
mode | streaming or static buffer. See MODE_STATIC and MODE_STREAM |
IllegalArgumentException |
---|
Flushes the audio data currently queued for playback.
Returns the configured audio data format. See ENCODING_PCM_16BIT and ENCODING_PCM_8BIT.
Returns the configured channel configuration. See CHANNEL_CONFIGURATION_MONO and CHANNEL_CONFIGURATION_STEREO.
Returns the configured number of channels.
Returns the maximum valid volume value. Volume values set above this one will be clamped at this value.
Returns the minimum buffer size required for the successful creation of an AudioTrack object to be created in the MODE_STREAM mode. Note that this size doesn't guarantee a smooth playback under load, and higher values should be chosen according to the expected frequency at which the buffer will be refilled with additional data to play.
sampleRateInHz | the sample rate expressed in Hertz. |
---|---|
channelConfig | describes the configuration of the audio channels. See CHANNEL_CONFIGURATION_MONO and CHANNEL_CONFIGURATION_STEREO |
audioFormat | the format in which the audio data is represented. See ENCODING_PCM_16BIT and ENCODING_PCM_8BIT |
Returns the minimum valid volume value. Volume values set under this one will be clamped at this value.
Returns the hardware output sample rate
Returns marker position expressed in frames.
Returns the playback state of the AudioTrack instance.
Returns the playback head position expressed in frames
Returns the current playback rate in Hz. Note that this rate may differ from the one set with setPlaybackRate(int) as the value effectively used is implementation-dependent.
Returns the notification update period expressed in frames.
Returns the configured audio data sample rate in Hz
Returns the state of the AudioTrack instance. This is useful after the AudioTrack instance has been created to check if it was initialized properly. This ensures that the appropriate hardware resources have been acquired.
Returns the type of audio stream this AudioTrack is configured for. Compare the result against STREAM_VOICE_CALL, STREAM_SYSTEM, STREAM_RING, STREAM_MUSIC or STREAM_ALARM
Pauses the playback of the audio data.
IllegalStateException |
---|
Starts playing an AudioTrack.
IllegalStateException |
---|
Releases the native AudioTrack resources.
Notifies the native resource to reuse the audio data already loaded in the native layer. This call is only valid with AudioTrack instances that don't use the streaming model.
Sets the loop points and the loop count. The loop can be infinite.
startInFrames | loop start marker expressed in frames |
---|---|
endInFrames | loop end marker expressed in frames |
loopCount | the number of times the loop is looped. A value of -1 means infinite looping. |
Sets the position of the notification marker.
markerInFrames | marker in frames |
---|
Sets the playback head position. The track must be stopped for the position to be changed.
positionInFrames | playback head position expressed in frames |
---|
Sets the listener the AudioTrack notifies when a previously set marker is reached or for each periodic playback head position update. Use this method to receive AudioTrack events in the Handler associated with another thread than the one in which you created the AudioTrack instance.
handler | the Handler that will receive the event notification messages. |
---|
Sets the listener the AudioTrack notifies when a previously set marker is reached or for each periodic playback head position update. Notifications will be received in the same thread as the one in which the AudioTrack instance was created.
Sets the playback sample rate for this track. This sets the sampling rate at which the audio data will be consumed and played back, not the original sampling rate of the content. Setting it to half the sample rate of the content will cause the playback to last twice as long, but will also result result in a negative pitch shift. The current implementation supports a maximum sample rate of 64kHz. Use getSampleRate() to check the rate actually used in hardware after potential clamping.
sampleRateInHz | the sample rate expressed in Hz |
---|
Sets the period for the periodic notification event.
periodInFrames | update period expressed in frames |
---|
Sets the specified left/right output volume values on the AudioTrack. Values are clamped to the (getMinVolume(), getMaxVolume()) interval if outside this range.
leftVolume | output attenuation for the left channel. A value of 0.0f is silence, a value of 1.0f is no attenuation. |
---|---|
rightVolume | output attenuation for the right channel |
Stops playing the audio data.
IllegalStateException |
---|
Writes the audio data to the audio hardware for playback.
audioData | the array that holds the data to play. |
---|---|
offsetInBytes | the offset expressed in bytes in audioData where the data to play starts. |
sizeInBytes | the number of bytes to read in audioData after the offset. |
Writes the audio data to the audio hardware for playback.
audioData | the array that holds the data to play. |
---|---|
offsetInShorts | the offset expressed in shorts in audioData where the data to play starts. |
sizeInShorts | the number of bytes to read in audioData after the offset. |
Is called before the object's memory is being reclaimed by the VM. This can only happen once the VM has detected, during a run of the garbage collector, that the object is no longer reachable by any thread of the running application.
The method can be used to free system resources or perform other cleanup
before the object is garbage collected. The default implementation of the
method is empty, which is also expected by the VM, but subclasses can
override finalize()
as required. Uncaught exceptions which are
thrown during the execution of this method cause it to terminate
immediately but are otherwise ignored.
Note that the VM does guarantee that finalize()
is called at most
once for any object, but it doesn't guarantee when (if at all) finalize()
will be called. For example, object B's finalize()
can delay the execution of object A's finalize()
method and
therefore it can delay the reclamation of A's memory. To be safe, use a
ReferenceQueue, because it provides more control
over the way the VM deals with references during garbage collection.
Returns the native frame count used by the hardware.
Sets the initialization state of the instance. To be used in an AudioTrack subclass constructor to set a subclass-specific post-initialization state.
state | the state of the AudioTrack instance |
---|