简体   繁体   English

如何播放 Jar 文件中资源文件夹中的 MP3 文件?

[英]How to play MP3 files that are in the resources folder in a Jar file?

I'm trying to access the MP3 files that are located inside the resources folder insidea jar file.我正在尝试访问位于 jar 文件中资源文件夹内的 MP3 文件。 I got some issues, they can't be readed.我遇到了一些问题,无法读取。

import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Logger;

import javazoom.jlgui.basicplayer.BasicPlayer;
import javazoom.jlgui.basicplayer.BasicPlayerException;

public class MP3 {

    private static final Logger logger = Logger.getLogger(Main.class.getName());


    private BasicPlayer player;


    /**
     * Constructor
     */
    public MP3() {
        player = new BasicPlayer();
    }

    public void changeSong(song) {

        // Change song now
        String pathToMp3 = System.getProperty("user.dir") + this.getClass().getResource(song);
        try {
            player.open(new URL("file:///" + pathToMp3));
            player.play();
            logger.info("Playing the song: " + song);
        } catch (MalformedURLException | BasicPlayerException e) {
            logger.info("Could not turn on MP3 music from: " + pathToMp3);
        }

    }

}

The pathToMp3 looks like this: pathToMp3 看起来像这样: 在此处输入图片说明

I have solved a similar issue with CSV read.我已经解决了 CSV 读取的类似问题。 Instead of reading from resources.而不是从资源中读取。 I read the resources as streams.我将资源作为流读取。

InputStream in = getClass().getResourceAsStream("/file.txt"); 
BufferedReader reader = new BufferedReader(new InputStreamReader(in));

But the problem here is that但这里的问题是

layer.open(new URL("file:///" + pathToMp3));

Takes the argument URL, not stream or string.采用参数 URL,而不是流或字符串。 Do you know how to stream MP3:s from the resources folder inside a executable JAR file?您知道如何从可执行 JAR 文件内的资源文件夹中流式传输 MP3:s 吗?

Update:更新:

       // Change song now
        String pathToMp3 = getClass().getResource(song).toString();
        InputStream in = getClass().getResourceAsStream(song);
        try {
            player.open(in);
            player.play();
            logger.info("Playing the song: " + song);
        } catch (BasicPlayerException e) {
            logger.info("Could not turn on MP3 music from: " + pathToMp3);
        }

Gives the error:给出错误:

在此处输入图片说明

I copied the BasicPlayerTest example from here and modified the play method to take an URL .我从这里复制了BasicPlayerTest示例并修改了 play 方法以获取URL

Then running the main works from the project and after the export in the jar.然后从项目中运行主要作品,并在 jar 中导出后运行。

Note that I copied the resources folder inside the src folder.请注意,我复制了 src 文件夹中的resources文件夹。

public static void main(String[] args) {
    BasicPlayerTest test = new BasicPlayerTest();
    test.play(test.getClass().getResource("/resources/test.mp3")); 
}

The url I am seeing in the log is:我在日志中看到的网址是:
jar:file:/C:/Temp/mp3.jar!/resources/test.mp3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM