读取mp3二进制数据以进行可视化

时间:2012-10-21 06:47:31

标签: java swing mp3 javasound

在我之前的帖子中,我在尝试阅读mp3文件时遇到了一些麻烦。现在我能够做到这一点,我希望能够使用java swing从mp3渲染数据。并且播放mp3并同时进行可视化会很棒。

我有二进制数据(我用它传送到输出流),但我不知道如何解释数据。

基本上,在LINE57附近,我需要对字节数组做什么才能将数据解释为db值?

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;

public class MainSound {

    public static void main(final String [] args) throws Exception {
        System.out.println("Running");        
        System.out.println(System.getProperty("java.version"));        
        final AudioFileFormat.Type [] types = AudioSystem.getAudioFileTypes();
        for (final AudioFileFormat.Type t : types) {
            System.out.println("Returning Type : " + t);
        } // End of the for //                
        final String PATH = "C:\\Users\\bbrown\\Downloads\\swing-hacks-examples-20060109\\Ch10-Audio\\75\\soundcloud2.mp3";             
        final File file = new File(PATH);
        final AudioInputStream in = AudioSystem.getAudioInputStream(new BufferedInputStream(new FileInputStream(file)));

        AudioInputStream din = null;
        final AudioFormat baseFormat = in.getFormat();
        final AudioFormat decodedFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
                baseFormat.getSampleRate(),
                16,
                baseFormat.getChannels(),
                baseFormat.getChannels() * 2,
                baseFormat.getSampleRate(),
                false);

        System.out.println("Channels : " + baseFormat.getChannels());                
        din = AudioSystem.getAudioInputStream(decodedFormat, in);        
        rawplay(decodedFormat, din);
        in.close();       
        System.out.println("Done");
    }    

    private static synchronized void rawplay(final AudioFormat targetFormat, final AudioInputStream din) throws IOException, LineUnavailableException {              
        final byte[] data = new byte[4096];
        final SourceDataLine line = getLine(targetFormat);               
        if (line != null) {
            System.out.println("Entering ...");
            // Start
            line.start();
            int nBytesRead = 0, nBytesWritten = 0;
            while (nBytesRead != -1) {
                nBytesRead = din.read(data, 0, data.length);
                if (nBytesRead != -1) {
                    // LINE57, HOW CAN INTERPRET this data for VISUALIZATION.
                    nBytesWritten = line.write(data, 0, nBytesRead);
                    System.out.println("... -->" + data[0] + " bytesWritten:" + nBytesWritten);
                }                                           
            } // End of while //            
            System.out.println("Done ...");
            // Stop
            line.drain();
            line.stop();
            line.close();
            din.close();
        } // End of the if //
    }

    private static synchronized SourceDataLine getLine(AudioFormat audioFormat) throws LineUnavailableException {
        SourceDataLine res = null;
        final DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat);
        res = (SourceDataLine) AudioSystem.getLine(info);
        res.open(audioFormat);
        return res;
    }

} // End of the class //

1 个答案:

答案 0 :(得分:9)

查看Extreme Media Player源代码,它是支持可视化的跨平台媒体播放器,当然也是用Java编写的。

对代码的研究应该有助于您了解如何读取正在播放的字节数据的分贝。 (我喜欢这个问题,因为我以前没见过这个问题。)

如下所示:

enter image description here

<强>更新

遗憾的是,Java本身并不支持MP3格式,但请看一下这个链接,显示我们JMF (Java Media Framework)这是j2SE的一个插件,可以在MP3 Plugin for JMF的帮助下启用MP3支持。< / p>