gpt4 book ai didi

java - 从 wav 文件播放原始 PCM 字节

转载 作者:行者123 更新时间:2023-11-30 03:39:38 28 4
gpt4 key购买 nike

我有一个用 16 位 PCM 编码的 wav 文件,我想将它转换为 PCM 短数组并使用音轨类播放原始数组。这就是我所做的:

        String fileName = "test.wav";
File f = new File(baseDir + File.separator + fileName);
InputStream is = null;
try {
is = new FileInputStream (f);
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
BufferedInputStream bis = new BufferedInputStream (is, 8000);
DataInputStream dis = new DataInputStream (bis); // Create a DataInputStream to read the audio data from the saved file
short[] music = new short[(int) f.length()];
int i = 0; // Read the file into the "music" array
try {
while (dis.available() > 0)
{
music[i] = dis.readShort(); // This assignment does not reverse the order
i++;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.i("123",""+Arrays.toString(music));

try {
dis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

at=new AudioTrack(AudioManager.STREAM_MUSIC, 8000, AudioFormat.CHANNEL_OUT_MONO,
AudioFormat.ENCODING_PCM_16BIT, 8000 /* 1 second buffer */,
AudioTrack.MODE_STREAM);
at.write(music, 0, (int)f.length());
at.play();

数组可以播放,但问题是我和播放原始wav文件比较时,发现它们输出的声音不一样。有人可以帮我解决这个问题吗?提前致谢!

最佳答案

您必须查找文件中的实际音频数据才能获取音频样本,请参阅 http://en.wikipedia.org/wiki/WAVhttps://ccrma.stanford.edu/courses/422/projects/WaveFormat/ .

16 位单声道 @ 8khz 的一秒将是 8000 * 2 * 1 字节 = 16000 字节

关于java - 从 wav 文件播放原始 PCM 字节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16002372/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com