2012-10-21 35 views
6

在我以前的文章中,我嘗試讀取mp3文件時遇到了一些麻煩。現在我可以做到這一點,我希望能夠使用java swing從mp3中呈現數據。這將是很好的播放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 // 

回答

9

看一看Extreme Meida Player源代碼,這是一個跨平臺的媒體播放器,支持可視化,當然,用Java編寫的。 對代碼的研究應該有助於您理解如何讀取正在播放的字節數據的分貝。 (+1非常喜歡這句話,因爲我還沒有看到這樣問過)

正如在這裏看到:

enter image description here

UPDATE:

它是一種可惜的Java本身並不支持MP3格式,但看看這個鏈接顯示我們JMF (Java Media Framework)這是一個插件j2SE,使MP3支持,額外的幫助MP3 Plugin for JMF

+2

+1的答案,真的不錯 – MadProgrammer

+0

@MadProgrammer +1謝謝你,我一直想編碼我自己的MediaPlayer,所以我在競爭對手中有一個詭異的高峯;) –

+1

好的例子,可讀的代碼,正是我所需要的。 MP3播放器仍然有點麻煩。我不得不包括一個mp3插件(外部)。 –