Why does Java Sound behave differently when I run from a .jar?

前端 未结 3 1391
清酒与你
清酒与你 2021-01-21 03:32

The play method below is from a class which, upon instantiation, reads a .wav file into a byte array called data, and stores the sound format in an

3条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-21 03:57

    Amplifying on @Anrew's and @Phil's answers, there's an outline of obtaining a Clipand playing it asynchronously below. There's a complete example here.

    AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(...);
    Clip clip = AudioSystem.getClip();
    clip.open(audioInputStream);
    ...
    // Play the sound in a separate thread.
    private void playSound() {
        Runnable soundPlayer = new Runnable() {
    
            @Override
            public void run() {
                try {
                    clip.setMicrosecondPosition(0);
                    clip.start();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        };
        new Thread(soundPlayer).start();
    }
    

提交回复
热议问题