java - 如何阅读 MP3

标签 java mp3

使用this post我能够找到如何读取 mp3 文件,但我仍然对如何实际使用它感到困惑。

File file = new File(filename);
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);

在这些声明之后,如何使用din?我知道循环看起来像这样:

while (/*What do I put here??*/){
    int currentByte = din.read();
}

换句话说,我只是问如何从 mp3 文件中读取整个字节数组,并在循环中一次检查一个字节。

最佳答案

AudioInputStream.read() returns -1 when it's out of data ,这样您就可以在发生这种情况时中断循环:

while (true){
    int currentByte = din.read();
    if (currentByte == -1) break;
    // Handling code
}

关于java - 如何阅读 MP3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31797500/

相关文章:

java - 使用 JGraphX 删除图中的重复顶点

java - HTML渲染算法

java - 检查数组是否包含特定元素的紧凑方式

android - 在android中使用MIC以mp3格式录制音频

http - 从 mp3/ogg 中删除精确的时间范围

python - 等待在 python 之外播放的歌曲完成

java - JAVA如何改变数据类型而不改变变量名

java - 如何在单击按钮时将图像发送到下一个屏幕

audio - 操纵音频以绕过内容 ID 检测

java - 将 WAV 转换为 MP3