天天看點

java wav檔案_用Java連接配接WAV檔案

這是我的代碼,連接配接四個wav檔案并生成wavAppended.wav。這個級聯檔案很好地在Windows Media Player中播放。

但通過PlaySound課程,隻能聽到one.wav。

誰能幫忙?

class PlaySound extends Object implements LineListener

{

File soundFile;

JDialog playingDialog;

Clip clip;

public void PlaySnd(String s) throws Exception

{

JFileChooser chooser = new JFileChooser();

soundFile = new File(s);

Line.Info linfo = new Line.Info(Clip.class);

Line line = AudioSystem.getLine(linfo);

clip = (Clip) line;

clip.addLineListener(this);

AudioInputStream ais = AudioSystem.getAudioInputStream(soundFile);

clip.open(ais);

clip.start();

}

public void update(LineEvent le)

{

LineEvent.Type type = le.getType();

playingDialog.setVisible(false);

clip.stop();

clip.close();

}

}

public class Main

{

public static void main(String[] args)

{

int i;

String wavFile[] = new String[4];

wavFile[0] = "D://one.wav";

wavFile[1] = "D://two.wav";

wavFile[2] = "D://three.wav";

wavFile[3] = "D://space.au";

AudioInputStream appendedFiles;

try

{

AudioInputStream clip0=AudioSystem.getAudioInputStream(new File(wavFile[0]));

AudioInputStream clip1=AudioSystem.getAudioInputStream(new File(wavFile[1]));

AudioInputStream clip3;

for (i=0;i<4;i++)

{

appendedFiles = new AudioInputStream(

new SequenceInputStream(clip0, clip1),

clip0.getFormat(),

clip0.getFrameLength() + clip1.getFrameLength());

AudioSystem.write(appendedFiles, AudioFileFormat.Type.WAVE, new File("D:\\wavAppended.wav"));

clip3 = AudioSystem.getAudioInputStream(new File("D:\\wavAppended.wav"));

clip0=clip3;

clip1 = AudioSystem.getAudioInputStream(new File(wavFile[i+2]));

}

PlaySound p = new PlaySound();

p.PlaySnd("D://wavAppended.wav");

}

catch (Exception e)

{

e.printStackTrace();

}

}

}