|
Build 1.1_r1 (from source) | |||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectandroid.media.AudioTrack
public class 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
be achieved by "pushing" the data to the AudioTrack object using the
write(byte[], int, int) or write(short[], int, int) method.
During construction, an AudioTrack object can be initialized with a given buffer.
This size determines how long an AudioTrack can play before running out of data.
| Nested Class Summary | |
|---|---|
static interface |
AudioTrack.OnMarkerReachedListener
Interface definition for a callback to be invoked when an AudioTrack has reached a notification marker set by setNotificationMarkerPosition(). |
static interface |
AudioTrack.OnPeriodicNotificationListener
Interface definition for a callback to be invoked for each periodic AudioTrack update during playback. |
| Field Summary | |
|---|---|
static int |
ERROR
Denotes a generic operation failure. |
static int |
ERROR_BAD_VALUE
Denotes a failure due to the use of an invalid value. |
static int |
ERROR_INVALID_OPERATION
Denotes a failure due to the improper use of a method. |
protected int |
mAudioFormat
The encoding of the audio samples. |
protected int |
mChannelConfiguration
The current audio channel configuration |
protected int |
mChannelCount
The number of input audio channels (1 is mono, 2 is stereo) |
protected int |
mDataLoadMode
The way audio is consumed by the hardware, streaming or static. |
protected AudioTrack.OnMarkerReachedListener |
mMarkerListener
The listener the AudioTrack notifies previously set marker is reached. |
protected Object |
mMarkerListenerLock
Lock to protect marker listener updates against event notifications |
protected int |
mNativeBufferSizeInBytes
Size of the native audio buffer. |
protected android.media.AudioTrack.NativeEventHandler |
mNativeEventHandler
Handler for events coming from the native code |
static int |
MODE_STATIC
Creation mode where audio data is transferred from Java to the native layer only once before the audio starts playing. |
static int |
MODE_STREAM
Creation mode where audio data is streamed from Java to the native layer as the audio is playing. |
protected AudioTrack.OnPeriodicNotificationListener |
mPeriodicListener
The listener the AudioTrack notifies periodically during playback. |
protected Object |
mPeriodicListenerLock
Lock to protect periodic listener updates against event notifications |
protected int |
mPlayState
Indicates the play state of the AudioTrack instance |
protected Object |
mPlayStateLock
Lock to make sure mPlayState updates are reflecting the actual state of the object. |
protected int |
mSampleRate
The audio data sampling rate in Hz. |
protected int |
mState
Indicates the state of the AudioTrack instance |
protected int |
mStreamType
The type of the audio stream to play. |
protected static int |
NATIVE_EVENT_MARKER
Event id for when the playback head has reached a previously set marker. |
protected static int |
NATIVE_EVENT_NEW_POS
Event id for when the previously set update period has passed during playback. |
static int |
PLAYSTATE_PAUSED
state of an AudioTrack this is paused |
static int |
PLAYSTATE_PLAYING
state of an AudioTrack this is playing |
static int |
PLAYSTATE_STOPPED
state of an AudioTrack this is stopped |
static int |
STATE_INITIALIZED
State of an AudioTrack that is ready to be used. |
static int |
STATE_NO_STATIC_DATA
State of a successfully initialized AudioTrack that uses static data, but that hasn't received that data yet. |
static int |
STATE_UNINITIALIZED
State of an AudioTrack that was not successfully initialized upon creation |
static int |
SUCCESS
Denotes a successful operation. |
| Constructor Summary | |
|---|---|
AudioTrack(int streamType,
int sampleRateInHz,
int channelConfig,
int audioFormat,
int bufferSizeInBytes,
int mode)
Class constructor. |
|
| Method Summary | |
|---|---|
protected void |
finalize()
Is called before the object's memory is being reclaimed by the VM. |
void |
flush()
Flushes the audio data currently queued for playback. |
int |
getAudioFormat()
Returns the configured audio data format. |
int |
getChannelConfiguration()
Returns the configured channel configuration. |
int |
getChannelCount()
Returns the configured number of channels. |
static float |
getMaxVolume()
Returns the maximum valid volume value. |
static float |
getMinVolume()
Returns the minimum valid volume value. |
protected int |
getNativeFrameCount()
Returns the native frame count used by the hardware |
static int |
getNativeOutputSampleRate()
Returns the hardware output sample rate |
int |
getNotificationMarkerPosition()
|
int |
getPlaybackHeadPosition()
|
int |
getPlayState()
Returns the playback state of the AudioTrack instance. |
int |
getPositionNotificationPeriod()
|
int |
getSampleRate()
Returns the configured audio data sample rate in Hz |
int |
getState()
Returns the state of the AudioTrack instance. |
int |
getStreamType()
Returns the type of audio stream this AudioTrack is configured for. |
void |
pause()
Pauses the playback of the audio data. |
void |
play()
Starts playing an AudioTrack. |
void |
release()
Releases the native AudioTrack resources. |
int |
reloadStaticData()
Notifies the native resource to reuse the audio data already loaded in the native layer. |
int |
setLoopPoints(int startInFrames,
int endInFrames,
int loopCount)
Sets the loop points and the loop count. |
void |
setMarkerReachedListener(AudioTrack.OnMarkerReachedListener listener)
Sets the listener the AudioTrack notifies when a previously set marker is reached. |
int |
setNotificationMarkerPosition(int markerInFrames)
|
void |
setPeriodicNotificationListener(AudioTrack.OnPeriodicNotificationListener listener)
Sets the listener the AudioTrack notifies periodically during playback. |
int |
setPlaybackHeadPosition(int positionInFrames)
Sets the playback head position. |
int |
setPlaybackRate(int sampleRateInHz)
Sets the playback sample rate for this track. |
int |
setPositionNotificationPeriod(int periodInFrames)
|
int |
setStereoVolume(float leftVolume,
float rightVolume)
Sets the specified left/right output volume values on the AudioTrack. |
void |
stop()
Stops playing the audio data. |
int |
write(byte[] audioData,
int offsetInBytes,
int sizeInBytes)
Writes the audio data to the audio hardware for playback. |
int |
write(short[] audioData,
int offsetInShorts,
int sizeInShorts)
Writes the audio data to the audio hardware for playback. |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
|---|
public static final int PLAYSTATE_STOPPED
public static final int PLAYSTATE_PAUSED
public static final int PLAYSTATE_PLAYING
public static final int MODE_STATIC
public static final int MODE_STREAM
public static final int STATE_UNINITIALIZED
public static final int STATE_INITIALIZED
public static final int STATE_NO_STATIC_DATA
public static final int SUCCESS
public static final int ERROR
public static final int ERROR_BAD_VALUE
public static final int ERROR_INVALID_OPERATION
protected static final int NATIVE_EVENT_MARKER
protected static final int NATIVE_EVENT_NEW_POS
protected int mState
protected int mPlayState
protected final Object mPlayStateLock
protected AudioTrack.OnMarkerReachedListener mMarkerListener
setMarkerReachedListener(OnMarkerReachedListener)protected final Object mMarkerListenerLock
protected AudioTrack.OnPeriodicNotificationListener mPeriodicListener
setPeriodicNotificationListener(OnPeriodicNotificationListener)protected final Object mPeriodicListenerLock
protected int mNativeBufferSizeInBytes
protected android.media.AudioTrack.NativeEventHandler mNativeEventHandler
protected int mSampleRate
protected int mChannelCount
protected int mStreamType
AudioManager.STREAM_VOICE_CALL, AudioManager.STREAM_SYSTEM,
AudioManager.STREAM_RING, AudioManager.STREAM_MUSIC and
AudioManager.STREAM_ALARM
protected int mDataLoadMode
protected int mChannelConfiguration
protected int mAudioFormat
#AudioFormat.ENCODING_PCM_8BIT,
#AudioFormat.ENCODING_PCM_16BIT| Constructor Detail |
|---|
public AudioTrack(int streamType,
int sampleRateInHz,
int channelConfig,
int audioFormat,
int bufferSizeInBytes,
int mode)
throws IllegalArgumentException
streamType - the type of the audio stream. See
AudioSystem.STREAM_VOICE_CALL, AudioSystem.STREAM_SYSTEM,
AudioSystem.STREAM_RING, AudioSystem.STREAM_MUSIC and
AudioSystem.STREAM_ALARMsampleRateInHz - 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 AudioFormat.CHANNEL_CONFIGURATION_MONO and
AudioFormat.CHANNEL_CONFIGURATION_STEREOaudioFormat - the format in which the audio data is represented.
See AudioFormat.ENCODING_PCM_16BIT and
AudioFormat.ENCODING_PCM_8BITbufferSizeInBytes - 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.mode - streaming or static buffer. See MODE_STATIC and MODE_STREAM
IllegalArgumentException| Method Detail |
|---|
public void release()
protected void finalize()
Object
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.
finalize in class Objectpublic static float getMinVolume()
public static float getMaxVolume()
public int getSampleRate()
public int getAudioFormat()
#AudioFormat.ENCODING_PCM_16BIT
and #AudioFormat.ENCODING_PCM_8BIT.
public int getStreamType()
AudioManager.STREAM_VOICE_CALL,
AudioManager.STREAM_SYSTEM, AudioManager.STREAM_RING,
AudioManager.STREAM_MUSIC or AudioManager.STREAM_ALARM
public int getChannelConfiguration()
#AudioFormat.CHANNEL_CONFIGURATION_MONO
and #AudioFormat.CHANNEL_CONFIGURATION_STEREO.
public int getChannelCount()
public int getState()
public int getPlayState()
AudioTrack.PLAYSTATE_STOPPED,
AudioTrack.PLAYSTATE_PAUSED,
AudioTrack.PLAYSTATE_PLAYINGprotected int getNativeFrameCount()
public int getNotificationMarkerPosition()
public int getPositionNotificationPeriod()
public int getPlaybackHeadPosition()
public static int getNativeOutputSampleRate()
public void setMarkerReachedListener(AudioTrack.OnMarkerReachedListener listener)
listener - public void setPeriodicNotificationListener(AudioTrack.OnPeriodicNotificationListener listener)
listener -
public int setStereoVolume(float leftVolume,
float rightVolume)
throws IllegalStateException
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
SUCCESS
IllegalStateExceptionpublic int setPlaybackRate(int sampleRateInHz)
getNativeOutputSampleRate()). Use getSampleRate() to
check the rate actually used in hardware after potential clamping.
sampleRateInHz -
SUCCESSpublic int setNotificationMarkerPosition(int markerInFrames)
markerInFrames - marker in frames
SUCCESS, ERROR_BAD_VALUE,
ERROR_INVALID_OPERATIONpublic int setPositionNotificationPeriod(int periodInFrames)
periodInFrames - update period in frames
SUCCESS, ERROR_INVALID_OPERATION
public int setPlaybackHeadPosition(int positionInFrames)
throws IllegalStateException
positionInFrames - playback head position in frames
SUCCESS, ERROR_BAD_VALUE
IllegalStateException - if the track is not in
the PLAYSTATE_STOPPED state.
public int setLoopPoints(int startInFrames,
int endInFrames,
int loopCount)
startInFrames - loop start marker in framesendInFrames - loop end marker in framesloopCount - the number of times the loop is looped.
A value of -1 means infinite looping.
SUCCESS, ERROR_BAD_VALUE
public void play()
throws IllegalStateException
IllegalStateException
public void stop()
throws IllegalStateException
IllegalStateException
public void pause()
throws IllegalStateException
IllegalStateException
public void flush()
throws IllegalStateException
IllegalStateException
public int write(byte[] audioData,
int offsetInBytes,
int sizeInBytes)
throws IllegalStateException
audioData - the array that holds the data to play.offsetInBytes - the offset in audioData where the data to play starts.sizeInBytes - the number of bytes to read in audioData after the offset.
IllegalStateException
public int write(short[] audioData,
int offsetInShorts,
int sizeInShorts)
throws IllegalStateException
audioData - the array that holds the data to play.offsetInShorts - the offset in audioData where the data to play starts.sizeInShorts - the number of bytes to read in audioData after the offset.
IllegalStateExceptionpublic int reloadStaticData()
SUCCESS, ERROR_BAD_VALUE,
ERROR_INVALID_OPERATION
|
Build 1.1_r1 (from source) | |||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||