2012-10-20 4 views
0

MP3 파일을로드하려고합니다. 내 클래스 경로에 jmf.jar (windows 버전)이 있고 이클립스를 통해 클래스를 실행하려고합니다. 하지만 실행하려고하면이 오류가 발생합니다. Java API 및 JMF를 사용하여 mp3 재생 /로드, 오류 지원되지 않는 형식

나는 다운로드 오라클 사이트에서 JMF의 버전을 설정합니다

JMF2.1.1e\lib

내가 (이클립스를 통해) 오라클 자바 7을 실행하고

오류 :

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 파일과 함께 사용해보십시오. 특히 여기에있는 오래된/간단한 파일 (http://pscode.org/media/#sound) (이 페이지는 현재 느립니다)과 함께 사용해보십시오. –

+0

mp3plugin.jar이 표시되지 않습니다. 위의 링크를 클릭하면 특별히 mp3plugin이없는 JMF 다운로드 사이트로 리디렉션됩니다. 또한 JMStudio를 사용하여 MP3를로드 할 수있었습니다. 그래서 그것은 내 코드 여야합니다. 나는 무엇을 바꿀 지 모른다. 또한 JavaDecoder 클래스가 내 클래스 경로에 없기 때문에 찾을 수 없습니다. –

답변

1

오라클 다운로드가 작동하지 않습니다. 나는이 사이트에서 MP3 플러그인을 다운로드 한 다음 내 클래스 패스에 플러그인을 추가했다. 이클립스와 함께 작동하지 않고.

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 형식은 항상 재생할 수 없습니다 wav 파일처럼 PCM_SIGNED로 당신이 그들을 포맷 경우에도 소리가 난다. 당신은 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

(http://www.javazoom.net/mp3spi/mp3spi.html)

누군가가 친절하게도으로 설정하려면였습니다 2017 년, 그리고 나는 mp3spi 패키지를 사용하여 MP3 인코딩/디코딩에 대한 지원을 추가 Google Code에 대한 의존성. 여기에 Gradle을 엔트리는 다음과 같습니다

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

그래서 또는 받는다는 해당 프로젝트에 당신의 MP3 지원을 제공해야합니다.