gpt4 book ai didi

Java - MP3 解码并将其存储到字节数组

转载 作者:行者123 更新时间:2023-11-29 06:06:40 24 4
gpt4 key购买 nike

我遇到了有关音频解码的问题。我有用于 mp3 解码的 SPIMP3 库,我正在尝试解码 mp3 并将它们存储到一个字节数组中。

事情是这样的,当我尝试解码一首 2 分钟的 mp3 歌曲时,它会给我以下字节:

[ -1, 0, 42, -115, -45, 0, 14 ... 等].

但是当我将 mp3 切成两半并尝试解码前半部分时,我得到以下字节:

[ 1, 0, 0, 65, -97, 135, -64, 32 ...等]

奇怪的是它们不匹配。这里唯一不同的是音频长度,但我正在解码两个相同的 mp3 样本的第一部分。

这是我的代码:

public void testPlay(String mp3) {
try {
File file = new File(mp3);
AudioInputStream in = AudioSystem.getAudioInputStream(file);
AudioInputStream din = null;
AudioFormat baseFormat = in.getFormat();
AudioFormat decodedFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
baseFormat.getSampleRate(),
16,
baseFormat.getChannels(),
baseFormat.getChannels() * 2,
baseFormat.getSampleRate(),
false);
din = AudioSystem.getAudioInputStream(decodedFormat, in);

play(decodedFormat, din);
spi(decodedFormat, in);
in.close();
} catch (Exception e) {
System.out.println("MP3");
}

}

private void play(AudioFormat targetFormat, AudioInputStream din) throws IOException, LineUnavailableException {

ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] data = new byte[4096];
SourceDataLine line = getLine(targetFormat);

int nBytesRead = 0, nBytesWritten = 0;
while (nBytesRead != -1) {
nBytesRead = din.read(data, 0, data.length);
if (nBytesRead != -1) {
nBytesWritten = line.write(data, 0, nBytesRead);
out.write(data, 0, 4096);
}

}

byte[] audio = out.toByteArray();

}

这是预料之中的事情还是我的代码有问题???

我怎样才能更改我的代码来为我的 mp3 的匹配部分获取相同的字节?

谢谢。

最佳答案

这一行应该是:

out.write(data, 0, nBytesRead);

关于Java - MP3 解码并将其存储到字节数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8316018/

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