我想用 AudioTrack 从 SD 卡播放音频文件。我试过这段代码:
int minBufferSize = AudioTrack.getMinBufferSize(8000,
AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_16BIT);
int bufferSize = 512;
AudioTrack at = new AudioTrack(AudioManager.STREAM_MUSIC, 8000,
AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_16BIT, minBufferSize,
AudioTrack.MODE_STREAM);
int i = 0;
byte[] s = new byte[bufferSize];
FileInputStream fin = new FileInputStream(path);
DataInputStream dis = new DataInputStream(fin);
at.play();
while ((i = dis.read(s, 0, bufferSize)) > -1)
{
at.write(s, 0, i);
}
at.stop();
at.release();
dis.close();
fin.close();
但它不能正确播放音频文件。它播放的不是原始音频,而是某种噪音。