我需要创建一个类来处理音频AudioTrack。
在这个课程中,必须有方法将录音带排列并按顺序同步播放。
有人可以帮助我吗?
我不知道同步出队并播放音频的每个部分。
我需要使用它,因为数据传递到byte []中的audiotrack不存档。所以我不能使用MediaPlayer。
答案 0 :(得分:1)
您可以使用以下代码......
bufferSize = AudioTrack.getMinBufferSize(sampleRateInHz,
AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT);
if (bufferSize != AudioTrack.ERROR_BAD_VALUE && bufferSize != AudioTrack.ERROR) {
audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
this.sampleRateInHz, AudioFormat.CHANNEL_OUT_MONO,
AudioFormat.ENCODING_PCM_16BIT, this.bufferSize,
AudioTrack.MODE_STREAM);
if(audioTrack!=null && audioTrack.getState() == AudioTrack.STATE_INITIALIZED){
Log.i(LOG_TAG,"Audio Track instance created buffer Size : "+this.bufferSize);
audioTrack.setPositionNotificationPeriod(320);
audioTrack.setPlaybackPositionUpdateListener(this);
audioTrack.play();
short[] tempBuf =new short[bufferSize/2];
// now write the code here to fill the tempBuf by reading from the file in shorts or bytes
audioTrack.write(tempBuf,0, tempBuf.length);
}else{
Log.e(LOG_TAG,"Unble to create AudioTrack instance");
}
} else {
Log.e(LOG_TAG, "Unable to get the minimum buffer size");
}