2012-01-22 2 views
2

Java에서 .AMR/.AAC 음악 파일을 재생할 프로그램을 작성해야합니다. AudioSystem 클래스로 같은 것을 만들었지 만 너무 무거워서 EAR 파일로 배송 할 수없는 웨이브 파일 만 지원합니다. 도와주세요!Java에서 .AMR/.AAC 파일 재생

package testSite; 

import java.io.File; 
import java.io.IOException; 
import javax.sound.sampled.AudioFormat; 
import javax.sound.sampled.AudioInputStream; 
import javax.sound.sampled.AudioSystem; 
import javax.sound.sampled.DataLine; 
import javax.sound.sampled.FloatControl; 
import javax.sound.sampled.LineUnavailableException; 
import javax.sound.sampled.SourceDataLine; 
import javax.sound.sampled.UnsupportedAudioFileException; 

public class AePlayWave extends Thread { 

    private String filename; 
    private Position curPosition; 
    private final int EXTERNAL_BUFFER_SIZE = 524288; // 128Kb 
    public static AePlayWave ob = null; 

    enum Position { 
     LEFT, RIGHT, NORMAL 
    }; 

    public static AePlayWave getInstance(String wavfile) { 
     if (ob == null) { 
      ob = new AePlayWave(wavfile); 
     } 
     return ob; 
    } 

    private AePlayWave(String wavfile) { 
     filename = wavfile; 
     curPosition = Position.NORMAL; 
    } 

    public void run() { 

     File soundFile = new File(filename); 
     if (!soundFile.exists()) { 
      System.err.println("Wave file not found: " + filename); 
      return; 
     } 

     AudioInputStream audioInputStream = null; 
     try { 
      audioInputStream = AudioSystem.getAudioInputStream(soundFile); 
     } catch (UnsupportedAudioFileException e1) { 
      e1.printStackTrace(); 
      return; 
     } catch (IOException e1) { 
      e1.printStackTrace(); 
      return; 
     } 

     AudioFormat format = audioInputStream.getFormat(); 
     SourceDataLine auline = null; 
     DataLine.Info info = new DataLine.Info(SourceDataLine.class, format); 

     try { 
      auline = (SourceDataLine) AudioSystem.getLine(info); 
      auline.open(format); 
     } catch (LineUnavailableException e) { 
      e.printStackTrace(); 
      return; 
     } catch (Exception e) { 
      e.printStackTrace(); 
      return; 
     } 

     if (auline.isControlSupported(FloatControl.Type.PAN)) { 
      FloatControl pan = (FloatControl) auline 
        .getControl(FloatControl.Type.PAN); 
      if (curPosition == Position.RIGHT) 
       pan.setValue(1.0f); 
      else if (curPosition == Position.LEFT) 
       pan.setValue(-1.0f); 
     } 

     auline.start(); 
     int nBytesRead = 0; 
     byte[] abData = new byte[EXTERNAL_BUFFER_SIZE]; 

     try { 
      while (nBytesRead != -1) { 
       nBytesRead = audioInputStream.read(abData, 0, abData.length); 
       if (nBytesRead >= 0) 
        auline.write(abData, 0, nBytesRead); 
      } 
     } catch (IOException e) { 
      e.printStackTrace(); 
      return; 
     } finally { 
      auline.drain(); 
      auline.close(); 
     } 

    } 
} 
+0

는 안녕하세요 - "된 .ear"파일은 "J2EE"와 "웹 응용 프로그램"을 의미한다. Q : 웹 응용 프로그램입니까? Q : 그렇다면 왜 Java 코드를 작성하여 "재생"해야합니까? * SERVER *에서 .mp3 파일 (또는 .amr 등)을 제공하기 위해 JSP (또는 무엇이든)를 작성하고 * BROWSER *가 오디오 클립을 재생하도록하는 것이 어떻습니까? – paulsm4

+0

죄송합니다 .. EAR에 의해 나는 자체 실행 가능한 java 파일을 의미합니다. Swing 응용 프로그램에 조금 익숙합니다 ... 백그라운드에서 음악을 재생해야하며 사운드 파일은 단일 실행 파일에 포함되어야합니다. –

답변

0

지금 오랜만하지만, 도움/도움을받을 것이라고 : 여기

내가 사용 무엇인가?
WAV를 마지막에 사용 했으므로 작동하고 있었지만 이제는 jaad 디코더를 사용하는 방법을 찾은 후 aac 재생이 올바르게 작동합니다.

Java play AAC encoded audio