2012-01-22 51 views
2

我需要構建一個程序來播放.​​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」 和 「Web應用程序」。問:這是一個Web應用程序嗎?問:如果是這樣,爲什麼你需要編寫任何Java代碼來「玩」任何東西?你爲什麼不寫一些JSP(或其他)來爲* SERVER *提供.mp3文件(或.amr等),並讓* BROWSER *播放音頻片段? – paulsm4

+0

對不起。我通過EAR我的意思是,一個自我可執行的Java文件。我對Swing應用程序有點新鮮......我需要在後臺播放音樂,並且需要將聲音文件作爲單個可執行文件的一部分。 –

回答

0

現在已經有一段時間,但它會帶來幫助/幫助?
我在最後使用了WAV,因爲它是我的工作,但現在aac播放工作正常後,我想出瞭如何使用jaad解碼器。

Java play AAC encoded audio