2012-10-20 44 views
0

我想加載MP3文件。我在我的類路徑中有jmf.jar(windows版本),並試圖通過Eclipse運行我的類。但是在嘗試運行時遇到此錯誤。使用Java API和JMF播放/加載mp3,錯誤不支持的格式

我下載並從Oracle站點設置這個版本的JMF的:

JMF2.1.1e\lib

我與Java 7甲骨文(通過Eclipse的)運行

錯誤:

javax.sound.sampled.UnsupportedAudioFileException: 
    could not get audio input stream from input stream 
    at 
javax.sound.sampled.AudioSystem.getAudioInputStream 
    (Unknown Source) 
    at 
org.berlin.sound.WaveformDisplaySimulator.main 
    (WaveformDisplaySimulator.java:47) 

這裏是代碼:

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

import javax.media.Codec; 
import javax.media.Format; 
import javax.media.PlugInManager; 
import javax.media.format.AudioFormat; 
import javax.sound.sampled.AudioFileFormat; 
import javax.sound.sampled.AudioInputStream; 
import javax.sound.sampled.AudioSystem; 


    public static void main(final String[] args) { 
     try { 

      System.out.println(System.getProperty("java.version")); 
      final String MP3 = "com.sun.media.codec.audio.mpa.JavaDecoder"; 
      Codec mp3 = (Codec) Class.forName(MP3).newInstance(); 

      final Format input1 = new AudioFormat(AudioFormat.MPEGLAYER3); 
      final Format input2 = new AudioFormat(AudioFormat.MPEG); 
      final Format output = new AudioFormat(AudioFormat.LINEAR); 
      PlugInManager.addPlugIn(
       "com.sun.media.codec.audio.mpa.JavaDecoder",     
       new Format[]{ input1, input2 }, 
       new Format[]{ output }, 
       PlugInManager.CODEC 
      ); 

      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\\Downloads\\soundcloud2.mp3"; 
      final File file = new File(PATH); 
      final AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new BufferedInputStream(new FileInputStream(file))); 

     } catch (final Exception e) { 
      e.printStackTrace(); 
     } 
    } // End of the method // 
+0

你是否還有MP3庫插件? http://www.oracle.com/technetwork/java/javase/download-137625.html –

+0

'無法從輸入流獲取音頻輸入流'不是所有的MP3都是相同的。嘗試使用其他MP3,尤其是舊的/簡單的MP3,可以在這裏找到(http://pscode.org/media/#sound)(該頁面目前很慢)。 –

+0

我沒有看到mp3plugin.jar。當我點擊上面的鏈接時,我會重定向到JMF下載站點,該站點並不專門具有mp3plugin。另外,我還發布了JMStudio並能夠加載MP3。所以它一定是我的代碼。我不知道要改變什麼。另外,JavaDecoder類不在我的類路徑中,我找不到它。 –

回答

1

我永遠無法獲得oracle下載工作。我最終從這個網站下載了一個MP3插件,然後在我的類路徑中添加了插件。這與Eclipse和沒有。

http://www.tritonus.org/plugins.html

而且,我沒有改變我的代碼。我能夠讀取mp3二進制數據,也可以輸出流。

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; 

http://www.tritonus.org/plugins.html 

    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) { 
        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 // 
    } 
0

的AudioInputStream無法打開MP3格式的聲音因爲他們被壓縮,即使你格式化爲PCM_SIGNED像他們不能總是扮演wav文件。您可能會喜歡jLayer,我從未見過它會崩潰

import javazoom.jl.player.advanced.AdvancedPlayer;

`

  try{ 
      File file = new File("C:/DMK.mp3"); 
      FileInputStream in = new FileInputStream(file); 
      AdvancedPlayer player = new AdvancedPlayer(in); 
      player.play(); 
     } 
     catch (Exception ex) { 
      ex.printStackTrace(); 
     } 

`

0

這是2017年,我通過使用mp3spi包添加的mp3編碼/解碼支持(http://www.javazoom.net/mp3spi/mp3spi.html

有人還跟其設置爲依賴Google Code。這裏的gradle這個條目:

compile group: 'com.googlecode.soundlibs', name: 'mp3spi', version: '1.9.5-1'

因此,這或行家等同應該給你的項目,你的MP3支持。