下面列出了android.media.AudioTrack#getState ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private AudioTrack createStreamingAudioTrack() {
final int channelConfig = getChannelConfig(mChannelCount);
int minBufferSizeInBytes
= AudioTrack.getMinBufferSize(mSampleRateInHz, channelConfig, mAudioFormat);
int bufferSizeInBytes = Math.max(MIN_AUDIO_BUFFER_SIZE, minBufferSizeInBytes);
AudioFormat audioFormat = (new AudioFormat.Builder())
.setChannelMask(channelConfig)
.setEncoding(mAudioFormat)
.setSampleRate(mSampleRateInHz).build();
AudioTrack audioTrack = new AudioTrack(mAudioParams.mAudioAttributes,
audioFormat, bufferSizeInBytes, AudioTrack.MODE_STREAM,
mAudioParams.mSessionId);
if (audioTrack.getState() != AudioTrack.STATE_INITIALIZED) {
Log.w(TAG, "Unable to create audio track.");
audioTrack.release();
return null;
}
mAudioBufferSize = bufferSizeInBytes;
setupVolume(audioTrack, mAudioParams.mVolume, mAudioParams.mPan);
return audioTrack;
}
public boolean startPlayer(int streamType, int sampleRateInHz, int channelConfig, int audioFormat) {
if (mIsPlayStarted) {
Log.e(TAG, "Player already started !");
return false;
}
mMinBufferSize = AudioTrack.getMinBufferSize(sampleRateInHz,channelConfig,audioFormat);
if (mMinBufferSize == AudioTrack.ERROR_BAD_VALUE) {
Log.e(TAG, "Invalid parameter !");
return false;
}
Log.d(TAG , "getMinBufferSize = "+mMinBufferSize+" bytes !");
mAudioTrack = new AudioTrack(streamType,sampleRateInHz,channelConfig,audioFormat,mMinBufferSize,DEFAULT_PLAY_MODE);
if (mAudioTrack.getState() == AudioTrack.STATE_UNINITIALIZED) {
Log.e(TAG, "AudioTrack initialize fail !");
return false;
}
mIsPlayStarted = true;
Log.d(TAG, "Start audio player success !");
return true;
}
public boolean startPlayer(int streamType, int sampleRateInHz, int channelConfig, int audioFormat) {
if (mIsPlayStarted) {
Log.e(TAG, "Player already started !");
return false;
}
mMinBufferSize = AudioTrack.getMinBufferSize(sampleRateInHz,channelConfig,audioFormat);
if (mMinBufferSize == AudioTrack.ERROR_BAD_VALUE) {
Log.e(TAG, "Invalid parameter !");
return false;
}
Log.d(TAG , "getMinBufferSize = "+mMinBufferSize+" bytes !");
mAudioTrack = new AudioTrack(streamType,sampleRateInHz,channelConfig,audioFormat,mMinBufferSize,DEFAULT_PLAY_MODE);
if (mAudioTrack.getState() == AudioTrack.STATE_UNINITIALIZED) {
Log.e(TAG, "AudioTrack initialize fail !");
return false;
}
mIsPlayStarted = true;
Log.d(TAG, "Start audio player success !");
return true;
}
public boolean setPlayer(int rate)
{
int bufferSizePlayer = AudioTrack.getMinBufferSize(rate, AudioFormat.CHANNEL_OUT_MONO, audioFormat);
Log.d("====buffer Size player ", String.valueOf(bufferSizePlayer));
player= new AudioTrack(AudioManager.STREAM_MUSIC, rate, AudioFormat.CHANNEL_OUT_MONO, audioFormat, bufferSizePlayer, AudioTrack.MODE_STREAM);
if (player.getState() == AudioTrack.STATE_INITIALIZED)
{
return true;
}
else
{
return false;
}
}
/**
* Initializes or reinitializes the audio track with the supplied format for playback
* while keeping the playstate. Keeps the current configuration and skips reinitialization
* if the new format is the same as the current format.
*/
public void init(MediaFormat format) {
Log.d(TAG, "init");
boolean playing = false;
if(isInitialized()) {
if(!checkIfReinitializationRequired(format)) {
// Set new format that equals the old one (in case we compare references somewhere)
mAudioFormat = format;
return;
}
playing = isPlaying();
pause();
stopAndRelease(false);
} else {
// deferred creation of the audio thread until its first use
mAudioThread = new AudioThread();
mAudioThread.setPaused(true);
mAudioThread.start();
}
mAudioFormat = format;
int channelCount = format.getInteger(MediaFormat.KEY_CHANNEL_COUNT);
int bytesPerSample = 2;
mFrameSize = bytesPerSample * channelCount;
mSampleRate = format.getInteger(MediaFormat.KEY_SAMPLE_RATE);
int channelConfig = AudioFormat.CHANNEL_OUT_DEFAULT;
switch(channelCount) {
case 1:
channelConfig = AudioFormat.CHANNEL_OUT_MONO;
break;
case 2:
channelConfig = AudioFormat.CHANNEL_OUT_STEREO;
break;
case 4:
channelConfig = AudioFormat.CHANNEL_OUT_QUAD;
break;
case 6:
channelConfig = AudioFormat.CHANNEL_OUT_5POINT1;
break;
case 8:
channelConfig = AudioFormat.CHANNEL_OUT_7POINT1;
}
mPlaybackBufferSize = mFrameChunkSize * channelCount;
mAudioTrack = new AudioTrack(
mAudioStreamType,
mSampleRate,
channelConfig,
AudioFormat.ENCODING_PCM_16BIT,
mPlaybackBufferSize, // at least twice the size to enable double buffering (according to docs)
AudioTrack.MODE_STREAM, mAudioSessionId);
if(mAudioTrack.getState() != AudioTrack.STATE_INITIALIZED) {
stopAndRelease();
throw new IllegalStateException("audio track init failed");
}
mAudioSessionId = mAudioTrack.getAudioSessionId();
mAudioStreamType = mAudioTrack.getStreamType();
setStereoVolume(mVolumeLeft, mVolumeRight);
mPresentationTimeOffsetUs = PTS_NOT_SET;
if(playing) {
play();
}
}