【问题标题】:JAVA - Sounds not playing after jar exportedJAVA - 导出 jar 后声音不播放
【发布时间】:2015-07-07 01:49:11
【问题描述】:

我的问题是导出我的 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;
        }
}
}

感谢您的帮助

【问题讨论】:

  • 你用什么程序来编译jar?您是否确保将声音包含在 jar 中?
  • 还有什么程序有吃、收、迷幻的音效?
  • 我使用 Eclipse 并通过 WinRar 检查了 jar --> 包含文件
  • 它们在一个名为 sound 的文件夹中?
  • 好的,非常感谢

标签: java audio


【解决方案1】:

【讨论】:

  • 很烦人的bug,我也遇到了
猜你喜欢
  • 2017-06-20
  • 1970-01-01
  • 2016-08-10
  • 1970-01-01
  • 2016-09-04
  • 1970-01-01
  • 1970-01-01
  • 2013-04-09
相关资源
最近更新 更多