Java - MP3 解码并将其存储到字节数组
我面临有关音频解码的问题。我有用于 mp3 解码的 SPIMP3 库,我正在尝试解码 mp3 并将它们存储到字节数组中。
事情是这样的,当我尝试解码 2 分钟的 mp3 歌曲时,它会给我以下字节:
[ -1, 0, 42, -115, -45, 0, 14 ... 等]。
但是当我将该 mp3 切成两半并尝试解码前半部分时,我得到以下字节:
[ 1, 0, 0, 65, -97, 135, -64, 32 ...etc]
奇怪的是它们不匹配。这里唯一不同的是音频长度,但我正在解码两个 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 的匹配部分的相同字节?
谢谢。
I am facing a problem regarding audio decoding. I have the SPIMP3 library for mp3 decoding and I am trying to decode mp3's and storing them to an array of bytes.
Here is the thing, when I try to decode a 2 minute mp3 song it gives me, for example, the following bytes:
[ -1, 0, 42, -115, -45, 0, 14 ... etc].
But when I cut that mp3 in half and try to decode the first half I get the following bytes:
[ 1, 0, 0, 65, -97, 135, -64, 32 ...etc]
The weird thing is that they don't match. The only thing that differs here is the audio length, but I am decoding the first part of both of the mp3 samples which is the same.
Here is my code:
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();
}
Is this something to be expected or is there a problem on my code???
How can I change my code to get the same bytes for the matching part of my mp3?
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这一行应该是:
this line should be: