JAVA - 在jar导出后声音没有播放

时间:2015-07-06 17:40:14

标签: java audio

我遇到的问题是,在导出我的java项目后,没有声音播放。

我在互联网上搜索,发现: Main.class.getResourceAsStream("fileloc");解决了这个问题

但是我的课使用了这个.. 问题在哪里?

public class Sounds {

private static Clip eat;
public static Clip collect;
private static Clip lsd;
private static AudioInputStream inputStream;
private static FloatControl gainControl;

public static void getSounds(){
    try{
        eat = getClip("/sound/collect/eat.wav");
        collect = getClip("/sound/collect/collect.wav");
        lsd = getClip("/sound/LSD.wav");
    } catch(LineUnavailableException | UnsupportedAudioFileException| IOException e){
        e.printStackTrace();
    }
}

private static Clip getClip(String loc) throws LineUnavailableException, UnsupportedAudioFileException, IOException{
    Clip c = AudioSystem.getClip();
    inputStream = AudioSystem.getAudioInputStream(Spielfeld.class.getResourceAsStream(loc));
    c.open(inputStream);
    gainControl = (FloatControl) c.getControl(FloatControl.Type.MASTER_GAIN);
    gainControl.setValue(+6.0f);
    c.setMicrosecondPosition(0);
    return c;
}

public static void playSound(final String url) {
        switch(url){
            case "collect/collect.wav": collect.start(); collect.setMicrosecondPosition(0); break;
            case "collect/eat.wav": eat.start(); eat.setMicrosecondPosition(0); break;
            case "LSD.wav": lsd.start(); lsd.setMicrosecondPosition(0); break;
        }
}
}

感谢您的帮助

1 个答案:

答案 0 :(得分:0)