玩其他的在Java中后,WAV文件中的一个 [英] Play WAV files one after the other in Java
问题描述
我想href=\"http://en.wikipedia.org/wiki/WAV\" rel=\"nofollow\"> WAV文件后,对方打了几个
为(字符串的文件:语音文件){
新AePlayWave(文件)。开始();
}
但是,播放它们都在同一时间。所以,我需要看起来像这样一个功能:
公共无效播放(矢量<串GT;语音文件);
该矢量包含文件,例如:test1.wav
,test2.wav
我一直在寻找的四个多小时,但我似乎无法找到一个有效的解决方案:(
我也试过WAV文件串联一个的AudioInputStream。它不给任何编译器错误,但声音完全搞砸了。 code:
公共静态的AudioInputStream CONCAT(矢量<串GT;文件)抛出UnsupportedAudioFileException,IOException异常{
总的AudioInputStream = AudioSystem.getAudioInputStream(新文件(files.get(0))); 的for(int i = 1; I< files.size();我++){
的AudioInputStream夹= AudioSystem.getAudioInputStream(新文件(files.get(I)));
总=新的AudioInputStream(新的SequenceInputStream(总,夹子),
total.getFormat(),
total.getFrameLength()+ clip.getFrameLength());
}
总回报;
}
修改
即使我尝试把前两个文件放在一起,它失败:
公共静态的AudioInputStream CONCAT(矢量<串GT;文件)抛出UnsupportedAudioFileException,IOException异常{
的AudioInputStream clip1 = AudioSystem.getAudioInputStream(新文件(files.get(0)));
的AudioInputStream CLIP2 = AudioSystem.getAudioInputStream(新文件(files.get(1))); 总的AudioInputStream =新的AudioInputStream(
新的SequenceInputStream(clip1,CLIP2)
clip1.getFormat(),
clip1.getFrameLength()+ clip2.getFrameLength()); 总回报;
}
这code是有点低的水平,但它的作品:
字节[]缓冲区=新的字节[4096];
对于(文件文件:文件){
尝试{
的AudioInputStream是= AudioSystem.getAudioInputStream(文件);
AudioFormat的格式= is.getFormat();
SourceDataLine的行= AudioSystem.getSourceDataLine(格式);
line.open(格式);
line.start();
而(is.available()大于0){
INT LEN = is.read(缓冲);
line.write(缓冲液,0,LEN);
}
line.drain(); // ** [DEIT] **等待缓冲关闭前行空
line.close();
}赶上(例外五){
e.printStackTrace();
}
}
基本上,你打开的AudioInputStream,读取数据并将数据写入的SourceDataLine。 写
办法阻止,因此会因此播放文件。
您可以尝试使用剪辑
为同一目的。
I'm trying to play a few WAV files after each other. I tried this method:
for (String file : audioFiles) {
new AePlayWave(file).start();
}
But that plays them all at the same time. So I need a function that looks like this:
public void play(Vector<String> audioFiles);
The vector contains the files, for example: "test1.wav"
,"test2.wav"
I have been looking for over four hours, but I can't seem to find a working solution :(
I also tried concatenating the WAV files to one AudioInputStream. It doesn't give any compiler errors, but the sound is totally messed up. Code:
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;
}
Edit
Even if I try to put the two first files together, it fails:
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;
}
This code is a bit low-level, but it works:
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();
}
}
Basically you open an AudioInputStream, read data and write it to a SourceDataLine. write
method is blocking, so it will play files consequently.
You can try to use Clip
for the same purpose.
这篇关于玩其他的在Java中后,WAV文件中的一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!