2012-02-10 130 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 

希望它能帮助。