编译时音频片段无法播放

时间:2014-10-14 12:51:23

标签: java audio methods resources

在最近关于StackOverflow的讨论中,我试图解决一个在编译后没有显示图像的imageicon问题,只有在eclipse中运行时才会出现。通过将我的资源文件夹的位置从我的项目文件夹更改为src文件夹,可以解决此问题。我还被教导使用以下代码获取我的资源:

ImageIcon img = new ImageIcon(getClass().getResource("/resources/picture.jpg"));

在没有看我如何编码我的音频方法的情况下,我问到在src文件夹中放置资源的方法对于所有类型的资源都是相同的,我被告知了。无论如何,现在我已经得到了我的音频代码,我有点不确定如何实现" getClass()。getResource()"排成一列。

现在我的音频按预期工作,当在eclipse中运行时,但在编译时它与使用的图像具有相同的问题。我知道问题非常相似,但我认为这是编码或缺乏编码的问题。任何指针或示例都将非常感激。感谢。

消息来源代码:

public static void menusong(){
try {
    AudioInputStream ais = AudioSystem.getAudioInputStream(new File("sounds/dre2.wav"));
    bgclip = AudioSystem.getClip();
    bgclip.open(ais);
    bgclip.start();

    try {
        Thread.sleep(33000);
    }
    catch (InterruptedException e) {
        e.printStackTrace();
    }
}
catch(Exception e) {
    e.printStackTrace();
    }
}

1 个答案:

答案 0 :(得分:1)

是的,明白了!而不是:

AudioInputStream ais = AudioSystem.getAudioInputStream(new file(" sounds / dre2.wav"));

我把它更改为:

AudioInputStream ais = AudioSystem.getAudioInputStream( classname .class.getClass()。getResource(" sounds / dre2.wav"));

现在所有人在编译时工作得非常好,无论如何都要感谢你的帮助,非常感谢。