用java压缩音乐的Jar?

发布于 2024-10-16 17:18:54 字数 911 浏览 1 评论 0原文

我尝试将音乐添加到我制作的应用程序中。首先,我尝试使用 .wav 文件,尽管它们变得太大,以至于应用程序变得太大而无法上传到任何地方。

因此,我将文件更改为 .mp3,尝试了 JMFJLayer,尽管它们都无法在可运行的 jar 上工作(即使它们可以工作)当我没有导出它们时很好)。

那么有人知道如何使用可运行的 jar 播放压缩音乐吗?

这是 JLayer 的代码,导出后它会在 f = new File(u.toURI()) 处停止工作,不会引发任何异常...

try {

    URL u = cl.getResource("New Beginnings.mp3");
    f = new File(u.toURI());

} catch (URISyntaxException e1) {
    e1.printStackTrace();
}

try {

    FileInputStream fis = new FileInputStream(f);
    p = new Player(fis);
    p.play();

} catch (Exception e) {
    System.out.println(e);
}

编辑:通过将上述代码更改为:

try {

      InputStream fis = ClassLoader.getSystemClassLoader().getResourceAsStream(temp+".mp3");
      p = new Player(fis);
      p.play();

   } catch (Exception e) {
      System.out.println(e);
   }

I've tried to add music to an application I've made. First I tried with .wav files though they became so huge that the application became too large to upload anywhere.

So I changed the files to .mp3, tried JMF and JLayer though both of them won't work on runnable jars (even if they work fine when I haven't exported them).

So anyone got any tips on how to play compressed music with a runnable jar?

Here's the code for JLayer, when exported it stops working at f = new File(u.toURI()) without throwing any exceptions...

try {

    URL u = cl.getResource("New Beginnings.mp3");
    f = new File(u.toURI());

} catch (URISyntaxException e1) {
    e1.printStackTrace();
}

try {

    FileInputStream fis = new FileInputStream(f);
    p = new Player(fis);
    p.play();

} catch (Exception e) {
    System.out.println(e);
}

Edit: Fixed with changing the above code to:

try {

      InputStream fis = ClassLoader.getSystemClassLoader().getResourceAsStream(temp+".mp3");
      p = new Player(fis);
      p.play();

   } catch (Exception e) {
      System.out.println(e);
   }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

撩起发的微风 2024-10-23 17:18:54

你把声音文件到底放在哪里了?

您应该在项目中创建一个新包并将资源放在那里,然后通过发送完整路径来读取文件:

例如。创建一个名为sounds的新包,然后:

  InputStream fis = ClassLoader.getSystemClassLoader().getResourceAsStream("/sounds/"+temp+".mp3");
  p = new Player(fis);
  p.play();

说实话,我没有尝试使用声音,但是当我使用图像时,这个问题发生在我身上。我把它们放在一个包里,一切正常。

Where did you place the sound file exactly?

You should create a new package inside your project and place the resources there, then read the file by sending a complete path:

ex. create a new package called sounds, then:

  InputStream fis = ClassLoader.getSystemClassLoader().getResourceAsStream("/sounds/"+temp+".mp3");
  p = new Player(fis);
  p.play();

To be honest, I didn't try it with sounds, but this problem happened to me when I used images. I placed them in a package, and everything worked fine..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文