2012-02-10 129 views
3

如何將兩個音頻文件混合到一個文件中,以便生成的文件可以同時播放兩個文件?請幫助..在這裏我在做什麼是我服用兩個文件,Concat的他們到另一個文件..但我想要的文件同時播放..使用混音器混合兩個音頻文件

private void saveAudio1() { 
    try {          

     AudioInputStream clip1 = AudioSystem.getAudioInputStream(file1); 
     AudioInputStream clip2 = AudioSystem.getAudioInputStream(file2); 
     Collection list=new ArrayList(); 

     AudioInputStream appendedFiles = 
       new AudioInputStream(
       new SequenceInputStream(clip1, clip2), 
       clip1.getFormat(), 
       clip1.getFrameLength() + clip2.getFrameLength()); 
     if (dlgOpenFile == null) { 
      dlgOpenFile = new FileDialog(this, "Save As...", FileDialog.SAVE); 
     } 
     if (cfgJMApps != null) { 
      nameFile = cfgJMApps.getLastOpenFile(); 
     } 
     if (nameFile != null) { 
      dlgOpenFile.setFile(nameFile); 
     } 

     dlgOpenFile.show(); 
     nameFile = dlgOpenFile.getFile(); 
     if (nameFile == null) { 
      return; 
     } 

     nameFile = dlgOpenFile.getDirectory() + nameFile; 
     if (cfgJMApps != null) { 
      cfgJMApps.setLastOpenFile(nameFile); 
     } 


     AudioSystem.write(appendedFiles, 
       AudioFileFormat.Type.WAVE, 
       new File(nameFile)); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 
+0

另請參閱http://stackoverflow.com/questions/26265575/playing-multiple-byte-arrays-simultaneously-in-java/ – 2016-10-08 00:20:51

回答

1

我只是找到了一個鏈接 http://www.jsresources.org/examples/AudioConcat.html

看起來他似乎在做它..源代碼可以在網頁上找到!希望這可以幫助你。

+1

他使用的是不存在於netbeans中的軟件包。所以我需要另一個來源.. import org.tritonus.share.sampled.TConversionTool; 這是包 – 2012-02-10 11:45:26

0

您需要從兩個流中按樣品讀取樣本,然後添加這些樣本(請注意溢出),然後將新添加的樣本存儲到新的AudioInputStream。請檢查here以瞭解如何將OutputStream轉換爲InputStream,屆時您將能夠製作另一個AudioInputStream並保存您的音頻文件。

+0

我應該同時播放這兩個文件...我可以這樣做嗎?我不確定.. plz幫助我.. thx – 2012-02-15 11:12:46

+0

對不起,遲到的答案。這是聲音的本質。採樣是將物理聲音轉換爲數字的意思。當你同時聽到兩個聲音時,物理和數學表示你實際上將第一個聲音的數字與第二個聲音的數字相加。現在,耳朵本身就起到緩衝作用,所以在添加後它實際上降低了總量。您可能會或可能不會執行該「降低」。 – RockyMM 2012-02-29 11:47:27

+0

或者更簡單的,你可以使用'Thread's,比如[here](http://www.anyexample.com/programming/java/java_play_wav_sound_file.xml)和[here](http://stackoverflow.com /問題/ 3093687 /播放WAV檔案 - 一後的,其他功能於JAVA)。 – RockyMM 2012-02-29 12:05:20

0

這是一類我做:

class MixedSound extends Thread { 
    protected AudioFormat format; //Both streams must have same format 
    protected AudioInputStream sound1; 
    protected AudioInputStream sound2; 
    protected static SourceDataLine ausgabe; 
    protected DataLine.Info data; 

    public MixedSound(String path1, String path2) { 
     try { 
     sound1 = AudioSystem.getAudioInputStream(new File(path1)); 
     sound2 = AudioSystem.getAudioInputStream(new File(path2)); 
     format=sound1.getFormat(); //I assume both streams have same format 
     data=new DataLine.Info(SourceDataLine.class,format); 
     ausgabe=(SourceDataLine) AudioSystem.getLine(data); 
     } catch (UnsupportedAudioFileException | IOException | LineUnavailableException bug) { 
      System.err.println(bug); 
     } 
    } 
    public synchronized void play() throws IOException, LineUnavailableException { 
     ausgabe.open(format,1024); 
     ausgabe.start(); 
     byte[] buffer1=new byte[1024]; 
     byte[] buffer2=new byte[1024]; 
     byte[] mixed=new byte[1024]; 
     int bytes_sound1=sound1.read(buffer1,0,buffer1.length); 
     int bytes_sound2=sound2.read(buffer2,0,buffer2.length); 
     while (bytes_sound1 != -1 || bytes_sound2 != -1) { 
      for (int i=0; i < mixed.length; i++) { 
       mixed[i]=(byte)Math.min(0.999f,((float)buffer1[i]+(float)buffer2[i])); //Mix them 
      } 
      ausgabe.write(mixed, 0, Math.max(bytes_sound1, bytes_sound2)); 
      buffer1=new byte[1024]; //Clean buffer 
      buffer2=new byte[1024]; //Clean buffer 
      bytes_sound1=sound1.read(buffer1,0,buffer1.length); 
      bytes_sound2=sound2.read(buffer2,0,buffer2.length); 
     } 
     ausgabe.drain(); 
     ausgabe.close(); 
    } 
    @Override 
    public synchronized void run() { 
     try { 
      play(); 
     } catch (IOException | LineUnavailableException ex) { 
      System.err.println(ex); 
     } 
    } 
} 

它通過增加它們的體積混合兩種聲音。

使用它這樣的:

MixedSound sound=new Sound("sound1.wav","sound2.wav"); 
sound.start(); //Play it 
System.out.println("Started playing sound"); //Do stuff at same time 

希望它能幫助。