天天看点

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

}

}

}