java:剪辑行无法正常工作

时间:2013-02-09 08:20:53

标签: java clip

这是我正在使用的代码。我使用了Clip类来播放剪辑。程序已经编译没有任何错误并且运行正常但我听不到声音。

import java.io.File;

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineEvent;
import javax.sound.sampled.LineListener;


public class ClipTest {

public static void main(String[] args) throws Exception {


File soundFile = new File("./1.wav");
AudioInputStream sound = AudioSystem.getAudioInputStream(soundFile);


DataLine.Info info = new DataLine.Info(Clip.class, sound.getFormat());
Clip clip = (Clip) AudioSystem.getLine(info);
clip.open(sound);


clip.addLineListener(new LineListener() {
  public void update(LineEvent event) {
    if (event.getType() == LineEvent.Type.STOP) {
      event.getLine().close();
      System.exit(0);
    }
  }
});


clip.start();
}
}      

1 个答案:

答案 0 :(得分:0)

我只是尝试你的代码。我认为你的错误是你的文件是空的或没有正确加载

我只是替换

File soundFile = new File("./1.wav");
AudioInputStream sound = AudioSystem.getAudioInputStream(soundFile);

InputStream inRequest = this.getClass().getResourceAsStream("1.wav");
AudioInputStream sound = AudioSystem.getAudioInputStream(inRequest);

这是新课程

public class ClipTest {

    public void run() throws UnsupportedAudioFileException, IOException, LineUnavailableException {
        InputStream inRequest = this.getClass().getResourceAsStream("batR.wav");
        AudioInputStream sound = AudioSystem.getAudioInputStream(inRequest);

        DataLine.Info info = new DataLine.Info(Clip.class, sound.getFormat());
        Clip clip = (Clip) AudioSystem.getLine(info);
        clip.open(sound);

        clip.addLineListener(new LineListener() {

            public void update(LineEvent event) {
                if(event.getType() == LineEvent.Type.STOP) {
                    event.getLine().close();
                    System.exit(0);
                }
            }
        });

        clip.start();

    }

    public static void main(String[] args) throws Exception {
        ClipTest clipTest = new ClipTest();
        clipTest.run();

    }
}