【问题标题】:Exporting JAR with Ressources, Audio won't play使用资源导出 JAR,音频不会播放
【发布时间】:2014-01-16 16:52:23
【问题描述】:

我做了一个小游戏,需要在上面播放背景音乐,我有图片,一个txt文件和一个音频文件,除了音频文件之外,它们在导出JAR后都可以工作。

这是我用来导入的代码:

图片:

(new ImageIcon(getClass().getResource("/data/131868.jpg"))

文本文件:

BufferedReader br = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream("/data/dictionnaire.txt")));

音频文件(我还包括了在搜索时找到的播放它的代码):

File f =new File(Main.class.getResource("/data/gmu.mp3").getFile());
final Player p=Manager.createRealizedPlayer(f.toURI().toURL());

        p.start();
        while(true){
            if(p.getMediaTime().getSeconds()==p.getDuration().getSeconds()){
                p.stop();
                p.setMediaTime(new Time(0));
                p.start();
            }
        }

基本上File 对象是:File f = new File("/data/gmu.mp3") 我只是添加了修改以使其看起来像其他对象...

它在 Eclipse 中确实有效,但在 JAR 中无效。

【问题讨论】:

    标签: java eclipse jar executable-jar


    【解决方案1】:

    您最好知道:文件只是文件的名称,而不是文件本身。和门牌号一样,它告诉你房子的位置,但不代表房子。

    所以,你可以这样使用它:

    InputStream is = this.getClass().getResourceAsStream("/data/gmu.mp3");

    File fi = new File(is);

    告诉我结果:)

    【讨论】:

    • 那我该如何表示文件本身呢?
    • 如果我使用它,我不能使用f.toURI().toURL(),因为FileReader 没有toURI 方法,我不能dast f
    • 给我结果:System.out.println(f.toURI().toURL());
    • 线程“main”java.lang.Error 中的异常:未解决的编译问题:方法 toURI() 未定义为 FileReader 类型
    • 另外,构造函数 File(InputStream) 没有定义
    【解决方案2】:

    解决方案

    在导出 jar 之前尝试将 gmu.mp3 放在源中。

    为什么?

    当您导出一个 jar 文件时,它会将源文件夹中的所有有趣代码打包。 File f = new File("/data/gmu.mp3") 只是将程序指向文件系统中名为 gmu.mp3 的文件。如果将gmu.mp3 放在源文件夹中并更新File 构造函数以反映新位置,则mp3 应该与所有代码一起打包到jar 中。

    让我知道进展如何-斯科特

    【讨论】:

    • 我将音频文件放在源文件夹 src 中并将 File 对象更新为 File("/data/gmu.mp3") 但我仍然得到相同的结果,我不知道这是如何工作的我已经指向我的资源文件夹了?
    • 我想确保您正在编译的 jar 实际上包含您指向的资源。可以从任何地方执行 jar。你指向的文件是相对于执行位置的,除非文件放在编译后的jar里面。
    • 音频文件位于data文件夹中,该文件夹位于src文件夹中,以及所有其他资源,当我导出java时,其他资源都在JAR中,它们都工作,除了音频
    【解决方案3】:

    java.io.File 将 jar 文件表示为“ThePacked.jar!/path/inside/file.mp3”,当用作文件时会出现问题。

    要读取 jar 文件的内容,您可以从 getResourceAsStream(String filename) 给出的 InputStream 中读取。

    要将文件用作真正的 java.io.File,(我所做的是)从 InputStream 中读取它并将其复制到 jar 之外的位置(例如,复制到临时文件)。

    【讨论】:

    • 我在stackoverflow.com/questions/19299470/… 的另一个答案包含“复制文件解决方案”的代码。
    • 所以根据您的链接,我将在final Player p=Manager.createRealizedPlayer(f.toURI().toURL()) 中使用的文件将是fo ??
    • 如果你的Manager可以接受一个InputStream,使用InputStream是最好的方法;否则,我想将 mp3 复制到临时文件,然后 final Player p=Manager.createRealizedPlayer(tempFileUrl)
    • 这是我添加的代码:File f =new File(Main.class.getResource("/tp/gmu.mp3").getFile()); InputStream fi = Main.class.getResourceAsStream("/tp/gmu.mp3"); File temp = File.createTempFile("temp", ""); OutputStream fo = new FileOutputStream(temp); byte[] b = new byte[1024]; int count = 0; while ((count = fi.read(b)) != -1) { fo.write(b, 0, count); } 我在Manager.createRealizedPlayer(fo.toURI().toURL()); 收到错误消息,toTUI is undefined for outpoutstream
    • 创建File temp 后,Manager.createRealizedPlayer(temp.toURI().toURL()); 应该可以工作了。
    猜你喜欢
    • 2016-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-20
    • 2023-03-05
    相关资源
    最近更新 更多