3

如何将两个音频文件混合到一个文件中,以便生成的文件可以同时播放两个文件?请帮助..这里我正在做的是我正在获取两个文件并将它们连接到另一个文件中..但我希望文件同时播放..

    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();
    }
}
4

4 回答 4

1

我刚刚找到了一个链接 http://www.jsresources.org/examples/AudioConcat.html

好像他在做。。源代码可以在页面上找到!希望这可以帮助你。

于 2012-02-10T11:21:12.477 回答
0

您需要从两个流中逐个读取样本,执行这些样本的添加(小心溢出),然后将新添加的样本存储到 newAudioInputStream中。在此处查看如何转换OutputStreamInputStream,何时您可以制作另一个AudioInputStream并保存您的音频文件。

于 2012-02-10T13:32:57.073 回答
0

@Vossi 说的是真的。使用 jresources.org 来帮助使用。您可以使用他们的示例: http: //www.jsresources.org/examples/MixingAudioInputStream.java.html。对于 org.tritonus.share.sampled.TConversionTool 包,如果您在使用他们的库时遇到问题,请下载他们的源代码:它是开源的,并尝试使用它。我试过了,它对我有用:D http://sourceforge.net/projects/tritonus/files/我很感激那些人!

于 2013-06-10T22:39:55.160 回答
-1

这是我做的一堂课:

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

希望能帮助到你。

于 2017-07-17T10:38:47.767 回答