2012-10-21 43 views
6

Trong bài trước, tôi gặp một chút rắc rối khi cố gắng đọc một tệp mp3. Bây giờ tôi có thể làm điều đó và tôi muốn có thể làm cho dữ liệu từ mp3 với java swing. Và nó sẽ là tốt đẹp để chơi mp3 và hình dung cùng một lúc.Đọc dữ liệu nhị phân mp3 để hiển thị

Tôi có dữ liệu nhị phân (mà tôi đã chuyển đến đầu ra) nhưng tôi không biết cách diễn giải dữ liệu.

Về cơ bản, vào khoảng LINE57, tôi cần làm gì với mảng byte để tôi có thể giải thích dữ liệu dưới dạng giá trị 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 // 

Trả lời

9

Hãy xem Extreme Meida Player mã nguồn là nền tảng Truyền thông Player hỗ trợ trực quan hóa và tất nhiên được viết bằng Java. Một nghiên cứu về mã sẽ giúp bạn hiểu cách đọc các decibel của dữ liệu byte đang được phát. (+1 yêu những câu hỏi như tôi havent thấy điều này hỏi trước đó)

Như đã thấy ở đây:

enter image description here

UPDATE:

của nó một điều đáng tiếc Java không natively hỗ trợ định dạng MP3, nhưng hãy xem liên kết này cho chúng tôi biết JMF (Java Media Framework) là một plugin cho j2SE cho phép hỗ trợ MP3, với sự trợ giúp của thêm MP3 Plugin for JMF

+2

+1 cho câu trả lời, thực sự tốt đẹp – MadProgrammer

+0

@MadProgrammer +1 cảm ơn bạn, tôi đã muốn mã MediaPlayer của riêng tôi vì vậy tôi đã có một đỉnh cao hé lộ ở các đối thủ cạnh tranh;) –

+1

Ví dụ tốt, mã có thể đọc được và chính xác những gì tôi cần. Vẫn có một chút rắc rối với máy nghe nhạc mp3. Tôi đã bao gồm một plugin mp3 (bên ngoài). –

Các vấn đề liên quan