cliptest.java

来自「Developing Games in Java 源代码」· Java 代码 · 共 39 行

JAVA
39
字号
import java.io.File;
import javax.sound.sampled.*;

/**
    An example of loading and playing a sound using a Clip.
    This complete class isn't in the book ;)
*/
public class ClipTest {

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

        // specify the sound to play
        // (assuming the sound can be played by the audio system)
        File soundFile = new File("../sounds/voice.wav");
        AudioInputStream sound =
            AudioSystem.getAudioInputStream(soundFile);

        // load the sound into memory (a Clip)
        DataLine.Info info = new DataLine.Info(Clip.class,
            sound.getFormat());
        Clip clip = (Clip)AudioSystem.getLine(info);
        clip.open(sound);

        // due to bug in Java Sound, explicitly exit the VM when
        // the sound has stopped.
        clip.addLineListener(new LineListener() {
            public void update(LineEvent event) {
                if (event.getType() == LineEvent.Type.STOP) {
                    event.getLine().close();
                    System.exit(0);
                }
            }
        });

        // play the sound clip
        clip.start();
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?