在IDEA中播放音频但是当编译成jar时它出错

时间:2017-04-29 15:19:11

标签: java audio

在IntelliJ IDEA中运行时,它可以正常运行,然而当我将其构建为工件时,音频会播放

play.close();

这是堆栈跟踪

  

java.lang.IllegalStateException:行已经关闭       在org.classpath.icedtea.pulseaudio.PulseAudioClip.close(PulseAudioClip.java:241)       在me.com.test.main.playAudio(main.java:37)       在me.com.test.main.main(main.java:50)

public static Boolean playAudio(File localSoundURL) {
    Clip play = null;
    try {
        AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(localSoundURL);
        play = AudioSystem.getClip();
        play.open(audioInputStream);

        FloatControl volume = (FloatControl) play.getControl(FloatControl.Type.MASTER_GAIN);
        volume.setValue(6.0f);

        play.start();
        play.drain();
    } catch(UnsupportedAudioFileException |IOException | LineUnavailableException ex) {
        ex.printStackTrace();
        return false;
    } finally {
        try {
            assert play != null;
            play.close();
        } catch (Exception exp) {
            exp.printStackTrace();
        } finally {
            return true;
        }
    }
}

public static void main(String[] args) throws InterruptedException {
    System.out.println(playAudio(new File(FileSystemView.getFileSystemView().getHomeDirectory()+"/Music/tahdah.wav")));
}

0 个答案:

没有答案