public class AudioTrack extends Object
Modifier and Type | Field and Description |
---|---|
static int |
PLAYSTATE_PAUSED
indicates AudioTrack state is paused
|
static int |
PLAYSTATE_PLAYING
indicates AudioTrack state is playing
|
static int |
PLAYSTATE_STOPPED
indicates AudioTrack state is stopped
|
Constructor and Description |
---|
AudioTrack()
Creates an AudioTrack.
Call release() to release native resources after using it. |
Modifier and Type | Method and Description |
---|---|
void |
flush()
Flushes the audio data currently queued for playback.
|
int |
getBufferSize()
Returns native buffer size.
|
int |
getPlayState()
Returns play state.
|
void |
pause()
Pauses the playback of the audio data.
|
void |
play()
Start playing
|
void |
release()
Releases the native AudioTrack resources.
|
void |
setAudioBitRate(int audioBitRate)
Set bit rate.
|
void |
setAudioSampleRate(int sampleRateInHz)
Set audio sample rate in Hertz.
|
void |
setChannelConfig(int channelConfig)
Set the configuration of the audio channels.
|
void |
stop()
Stops playing the audio data.
|
int |
write(byte[] audioData,
int offset,
int size)
Writes the audio data to the audio sink for playback, block until all
data has been written to the audio sink.
|
public static final int PLAYSTATE_STOPPED
public static final int PLAYSTATE_PAUSED
public static final int PLAYSTATE_PLAYING
public AudioTrack() throws IOException
release()
to release native resources after using it.IOException
- if failed to create AudioTrackpublic void setAudioSampleRate(int sampleRateInHz) throws IllegalArgumentException
sampleRateInHz
- sample rate. Normally use 8000
,
16000
, 22050
, 44100
,
48000
. Default is 16000
.IllegalArgumentException
public void setChannelConfig(int channelConfig) throws IllegalArgumentException
channelConfig
- audio channel configuration. See
AudioFormat.CHANNEL_MONO
and
AudioFormat.CHANNEL_STEREO
. Default is
CHANNEL_MONO
IllegalArgumentException
public void setAudioBitRate(int audioBitRate) throws IllegalArgumentException
audioBitRate
- bit per sample. Normally use 8
,
16
. Default is 16
IllegalArgumentException
public int getBufferSize()
public int getPlayState()
PLAYSTATE_STOPPED
,
PLAYSTATE_PAUSED
,
PLAYSTATE_PLAYING
public int write(byte[] audioData, int offset, int size) throws IOException
audioData
- the array that holds the data to play.offset
- the start offset in the audioData
size
- the number of bytes to writeIOException
- if an I/O error occurs or player has been stopped.public void play() throws IOException
IOException
- if an I/O error occurspublic void pause() throws IOException
play()
will play this
data back. See flush()
to discard this data.IOException
- if an I/O error occurspublic void flush() throws IOException
IOException
- if an I/O error occurspublic void stop() throws IOException
pause()
, followed by flush()
to discard audio data that
hasn't been played back yet.IOException
- if an I/O error occurspublic void release() throws IOException
IOException
- if an I/O error occurs