开发者

concatenate 2 byte arrays and then convert to an audio stream

开发者 https://www.devze.com 2023-04-05 02:27 出处:网络
The following is the code that reads audio data from 2 audio input streams into a byte array. import javax.sound.sampled.*;

The following is the code that reads audio data from 2 audio input streams into a byte array.

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

}

From the above code i have a byte array1,array2 for ais_1,ais_2 . Is there any way to conc开发者_JAVA技巧atenate these 2 byte arrays ( arr_1,arr_2 ) and then convert them back to an audio stream ? I want to concatenate 2 audio files.


Once you have the two byte arrays in hand (see my comment), you can concatenate them into a third array like this:

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);

Still not a complete answer, sorry, as this array is just the sample data - you still need to write out a header followed by the data. I didn't see any way to do this with the AudioSystem api.

Edit: try this:

Join two WAV files from Java?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号