2010-06-22 60 views
0

我試圖在彼此之後播放幾個WAV文件。我試過這種方法:在Java中一個接一個地播放WAV文件

for (String file : audioFiles) { 
    new AePlayWave(file).start(); 
} 

但是,它在同一時間播放它們。所以,我需要的功能,看起來像這樣:

public void play(Vector<String> audioFiles); 

的載體包含的文件,例如:"test1.wav""test2.wav"

我一直在尋找的四個多小時,但我似乎無法找到。一個可行的解決方案:(

我也試過WAV文件串聯到一個的AudioInputStream它不給任何編譯器錯誤,但聲音是完全搞砸代碼:

public static AudioInputStream concat(Vector<String> files) throws UnsupportedAudioFileException, IOException { 
    AudioInputStream total = AudioSystem.getAudioInputStream(new File(files.get(0))); 

    for (int i = 1; i < files.size(); i++) { 
     AudioInputStream clip = AudioSystem.getAudioInputStream(new File(files.get(i))); 
     total = new AudioInputStream(new SequenceInputStream(total, clip), 
            total.getFormat(), 
            total.getFrameLength() + clip.getFrameLength()); 
    } 
    return total; 
} 

編輯

即使我嘗試把兩個第一文件一起,它失敗:

public static AudioInputStream concat(Vector<String> files) throws UnsupportedAudioFileException, IOException { 
    AudioInputStream clip1 = AudioSystem.getAudioInputStream(new File(files.get(0))); 
    AudioInputStream clip2 = AudioSystem.getAudioInputStream(new File(files.get(1))); 

    AudioInputStream total = new AudioInputStream(
     new SequenceInputStream(clip1, clip2), 
     clip1.getFormat(), 
     clip1.getFrameLength() + clip2.getFrameLength()); 

    return total; 
} 
+0

你的方法來嘗試肯定串聯的文件,編碼,因爲你在每次迭代覆蓋'total'將無法正常工作。 – 2010-06-22 13:52:18

+0

是的,但我用總和下一個剪輯的組合覆蓋它 或仍然有問題嗎? (它聽起來像是我覆蓋它:)) 我該如何解決它? – Berty 2010-06-22 14:21:09

回答

2

此代碼是有點低的水平,但它的工作原理:

byte[] buffer = new byte[4096]; 
    for (File file : files) { 
     try { 
      AudioInputStream is = AudioSystem.getAudioInputStream(file); 
      AudioFormat format = is.getFormat(); 
      SourceDataLine line = AudioSystem.getSourceDataLine(format); 
      line.open(format); 
      line.start(); 
      while (is.available() > 0) { 
       int len = is.read(buffer); 
       line.write(buffer, 0, len); 
      } 
      line.drain(); //**[DEIT]** wait for the buffer to empty before closing the line 
      line.close(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

基本上你打開一個AudioInputStream,讀取數據並將其寫入SourceDataLine。 write方法阻塞,因此它會播放文件。

您可以嘗試使用Clip來達到同樣的目的。

+0

這個工作,但是聲音被在每個文件的結尾削減每次 如果你要播放的文件: hello.wav and_welcome.wav to_stackoverflow.wav 你聽到: HEL和WELCO到stackove 任何想法如何解決這個問題? – Berty 2010-06-24 08:39:39

+0

@Berty:在line.close()之前添加line.drain() – 2010-06-24 15:50:48

+0

完美伎倆 thx如此之多 它現在聽起來很完美:D – Berty 2010-06-25 09:16:59

0

最終的答案(含排水):

public static void play(ArrayList<String> files){ 
    byte[] buffer = new byte[4096]; 
    for (String filePath : files) { 
     File file = new File(filePath); 
     try { 
      AudioInputStream is = AudioSystem.getAudioInputStream(file); 
      AudioFormat format = is.getFormat(); 
      SourceDataLine line = AudioSystem.getSourceDataLine(format); 
      line.open(format); 
      line.start(); 
      while (is.available() > 0) { 
       int len = is.read(buffer); 
       line.write(buffer, 0, len); 
      } 
      line.drain(); 
      line.close(); 
     } catch (Exception ex) { 
      ex.printStackTrace(); 
     } 
    } 
}