声音会在eclipse中导出的jar文件播放,但不 [英] sound will play in eclipse but not in exported jar file

查看:316
本文介绍了声音会在eclipse中导出的jar文件播放,但不的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我运行JAR声音不玩了,但它确实当我在Eclipse中运行它。

Sound does not play when I run the JAR, but it does when I run it in eclipse.

下面是我加载剪辑:(该文件是从罐子的目录加载,而不是从瓶子内)

Here is where I load the clips: (the files are loaded from the directory of the jar, Not from within the jar)

public void init(){
    System.out.println("grabbing Music");
    String currentDir = new File("").getAbsolutePath();
    name=new File(currentDir+"\\music\\").list();
    clip=new Clip[name.length];
    soundFile=new File[name.length];
    for(int x=0;x<name.length;x++){
        System.out.println(currentDir+"\\music\\"+name[x]);
        try {
            soundFile[x]= new File(currentDir+"\\music\\"+name[x]);
            AudioInputStream sound = AudioSystem.getAudioInputStream(soundFile[x]);
            DataLine.Info info= new DataLine.Info(Clip.class, sound.getFormat());
            clip[x] = (Clip) AudioSystem.getLine(info);
            clip[x].open(sound);
            clip[x].addLineListener(new LineListener(){
                public void update(LineEvent event) {
                    if (event.getType() == LineEvent.Type.STOP) {
                        event.getLine().close();
                    }
                }
            });
        } catch (LineUnavailableException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (UnsupportedAudioFileException e) {
            e.printStackTrace();
        }
    }

}

在Eclipse中运行时,我没有得到任何错误。不应该有一个无效目录错误的可能性,所以有什么不好?

I do not get any errors when running it in Eclipse. There should be no possibility of an invalid directory error, so what is wrong?

- 当瓶子在CMD运行我没有得到任何错误,也没有踪迹,所以它加载的文件,它只是心不是运行它时,clip.start()被调用。因此,IM加载的文件的方式不符合可运行罐子兼容。

-When the jar is run in CMD i get no errors nor stacktraces, so it IS loading the files, it just isnt running it when the clip.start() is called. So the way im loading the files in is not compatible with runnable jars.

编辑:我觉得我加载音频错的,因此为什么我粘贴了code我用来加载中的文件在我的搜索我还没有看到任何人使用文件中的声音文件加载。不知道这是什么问题?

edit: I feel like I am loading the audio wrong, hence why I pasted the code I used to load the files in. In my searches I haven't seen anyone use File to load in a sound file. Wonder if that is the problem?

- 也切换到嵌入的资源也不起作用。

-Also switching to embedded resources ALSO does not work.

推荐答案

我这种挣扎也是如此。您需要使用

I struggled with this as well. You need to use

的getClass()。getSystemResource(pathToResourceFromClass)

例如,如果你的项目结构是这样的:

For example, if your project structure is like the following:

-ProjectName

-ProjectName

----- SRC

-----src

---------- SomeClass.java

----------SomeClass.java

-----图片

---------- SomeImage.png

----------SomeImage.png

再从SomeClass.java,你可以使用

Then from SomeClass.java, you would use

getClass().getSystemResource("images/SomeImage.png")

获得网​​址对象它引用该文件。然后,您可以传递网​​址对象的ImageIcon 的构造函数,如果你想要的。

to obtain the URL object which references that file. You could then pass that URL object to the constructor of ImageIcon if you want.

注意的getClass()。getResource()方法的图像或声音文件在一个jar文件不起作用!的我已经花了这么多时间,而的getResource()不起作用。你必须使用 getSystemResource(..)的JAR文件中的资源。

Note that getClass().getResource() does not work for images or sound files in a jar file!!! I've spent many hours on this, and getResource() does not work. You have to use getSystemResource(..) for resources within a jar file.

让我知道,如果您对此有任何问题。我很乐意澄清任何混乱的点。

Let me know if you have any questions about this. I'd be glad to clarify any confusing points.

这篇关于声音会在eclipse中导出的jar文件播放,但不的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆