我正在尝试使用 AudioTrack 流式传输音乐。曲目播放,但音频以一半的速度播放。这首歌就像变成了慢动作。
try{
AudioTrack track= new AudioTrack( AudioManager.STREAM_MUSIC,
44100,
android.media.AudioFormat.CHANNEL_CONFIGURATION_MONO,
android.media.AudioFormat.ENCODING_PCM_16BIT,
android.media.AudioTrack.getMinBufferSize( 44100,
android.media.AudioFormat.CHANNEL_CONFIGURATION_STEREO,
android.media.AudioFormat.ENCODING_PCM_16BIT ),
AudioTrack.MODE_STREAM );
System.out.println("Min buffer: " + android.media.AudioTrack.getMinBufferSize( 44100,
android.media.AudioFormat.CHANNEL_CONFIGURATION_STEREO,
android.media.AudioFormat.ENCODING_PCM_16BIT ));
int cnt;
int totalWrite = 0;
boolean play = true;
byte buff[]=new byte[16384];
while((cnt=CircularByteBuffer.getInstance().getInputStream().read(buff))>0){
totalWrite += cnt;
System.out.println("Writing: " + cnt);
track.write(buff, 0, cnt);
if ( totalWrite > 60000 && play ){
track.play();
play = false;
}
}
}catch (Exception e)
{
}//end catch
在 CircularByteBuffer 中,字节被写入另一个线程并在这个线程上读取。这首歌持续播放,没有任何停顿,但播放速度很慢。我不知道它可能是什么。有任何想法吗?