使用混音器混合两个音频文件
如何将两个音频文件混合到一个文件中,以便生成的文件可以同时播放两个文件?请帮忙..这里我正在做的是我正在获取两个文件并将它们连接到另一个文件中..但我希望该文件同时播放..
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();
}
}
how can i mix two audio files into one file so that the resultant file can play two files simultaneously? please help.. here what i am doing is that i am taking two files and concat them into another file.. but i want the file to be played simultaneously..
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();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是我制作的一个类:
它通过添加音量来混合两种声音。
像这样使用它:
希望有帮助。
Here is a class I made :
It mixes two sounds by adding their volumes.
Use it like that :
Hope it helps.
我刚刚找到了一个链接
http://www.jsresources.org/examples/AudioConcat.html
看来他正在做..页面上可以找到源代码!希望这对您有帮助。
I just found a link
http://www.jsresources.org/examples/AudioConcat.html
It seems like he is doing it.. The source code can be found on the page! Hope this helps you out.
@Vossi 说的是真的。使用 jresources.org 来帮助使用。您可以使用他们的示例: http://www.jsresources.org/examples/MishingAudioInputStream.java .html。对于 org.tritonus.share.sampled.TConversionTool 包,如果您在使用其库时遇到问题,请下载其源代码:它是开源的,并尝试使用它。我尝试过,它对我有用:D http://sourceforge.net/projects/tritonus/files/ 我很感谢那些人!
what @Vossi said is true. Use jresources.org to help use. You could use their example: http://www.jsresources.org/examples/MixingAudioInputStream.java.html. For the org.tritonus.share.sampled.TConversionTool package, if you have problems using their library, then download their source code: it's open source, and try using it. I tried it and it worked for me :D http://sourceforge.net/projects/tritonus/files/ I'm grateful for those guys!
您需要从两个流中逐个读取样本,执行这些样本的添加(小心溢出),然后将新添加的样本存储到新的
AudioInputStream
中。检查此处了解如何转换OutputStream
到InputStream
,当您能够创建另一个AudioInputStream
并保存音频文件时。You need to read sample by sample from both streams, perfrom addition of those samples (be carefull of overflow) and then store new added sample to new
AudioInputStream
. Check here to see how to convertOutputStream
toInputStream
, when you'll be able to make anotherAudioInputStream
and save your audio file.