【发布时间】:2016-12-20 13:50:17
【问题描述】:
我正在尝试将音频文件转换为标准格式并读取字节。
我要做的第一件事是:
static void main(String [] args)
{
File file = new File("/path/to/my/file.mp3");
AudioInputStream source = AudioSystem.getAudioInputStream(file);
AudioFormat sourceFormat = source.getFormat();
AudioFormat convertFormat = new AudioFormat(
AudioFormat.Encoding.PCM_SIGNED,
sourceFormat.getSampleRate(),
16,
sourceFormat.getChannels(),
sourceFormat.getChannels() * 2,
sourceFormat.getSampleRate(),
false);
AudioInputStream convert1AIS = AudioSystem.getAudioInputStream(convertFormat, source);
AudioFormat targetFormat = new AudioFormat(
44100,
8,
1,
true,
true);
AudioInputStream target = AudioSystem.getAudioInputStream(targetFormat, convert1AIS);
byte[] buffer = new byte[4096];
//Here come strange things
target.read(buffer,0, buffer.length);
}
每次我启动程序时,缓冲区都包含不同的字节,即前 10 个字节,我在调试器中看到的这些字节永远不会相同。怎么了?
我添加了以下库: jl1.0.1.jar, jtransform-2.4.jar, mp3spi1.9.5.jar, tritonus_remaining-0.3.6.jar, tritonus_share.jar
操作系统。 Ubuntu 14.04
java 版本“1.8.0_111” Java(TM) SE 运行时环境 (build 1.8.0_111-b14) Java HotSpot(TM) 64 位服务器 VM(内部版本 25.111-b14,混合模式)
【问题讨论】:
-
我没有使用过这个 API,但看起来你正试图将 MP3 读取为 PCM,但这是行不通的。您需要使用 MP3 编解码器将 MP3 解码为 PCM,然后才能对其进行任何其他操作。
-
你已经添加了所有这些库,但是你在哪里使用它们。 mp3 需要专门阅读 audioinputstream 不支持它,这就是为什么您添加库以开头不是吗?去看看其他一些使用这些库来阅读 mp3 的例子,否则我稍后会回来
标签: java audio-streaming