将2 audio input streams
的音频数据读入字节数组的代码如下:
import javax.sound.sampled.*;
import java.io.*;
class tester {
public static void main(String args[]) throws IOException {
try {
Clip clip_1 = AudioSystem.getClip();
AudioInputStream ais_1 = AudioSystem.getAudioInputStream( new File("D:\UnderTest\wavtester_1.wav") );
clip_1.open( ais_1 );
Clip clip_2 = AudioSystem.getClip();
AudioInputStream ais_2 = AudioSystem.getAudioInputStream( new File( "D:\UnderTest\wavtester_2.wav") );
clip_2.open( ais_2 );
byte arr_1[] = new byte[ais_1.available()]; // not the right way ?
byte arr_2[] = new byte[ais_2.available()];
ais_1.read( arr_1 );
ais_2.read( arr_2 );
} catch( Exception exc ) {
System.out.println( exc );
}
}
}
从上面的代码我有一个字节array1,array2
为ais_1,ais_2
。是否有任何方法连接这些2字节数组(arr_1,arr_2
),然后将它们转换回音频流?我想连接两个音频文件
一旦有了这两个字节数组(参见我的注释),就可以将它们连接成第三个数组,如下所示:
byte[] arr_combined = new byte[arr_1.length + arr_2.length];
System.arraycopy(arr_1, 0, arr_combined, 0, arr_1.length);
System.arraycopy(arr_2, 0, arr_combined, arr_1.length, arr_2.length);
仍然不是一个完整的答案,对不起,因为这个数组只是样本数据-您仍然需要写一个头,后面跟着数据。在AudioSystem api中,我没有看到任何方法可以做到这一点。
Edit: try this:
从Java加入两个WAV文件?