以下是从两个音频输入流中读取音频数据到字节数组的代码。
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
。有没有办法将这两个字节数组(arr_1,arr_2
)连接起来,然后将它们转换回音频流?我想要连接两个音频文件。
.available()
不一定会返回流中可用的总字节数。相反,应该使用ais_1.frameLength * ais_1.frameSize
。 - MusiGenesis