0

I did this small Java project that in it's turn opens different MP3 files. For that I downloaded the JLayer 1.0.1 library and added it to my project. I also added the MP3 files to a package on my project -as well as some JPG images- so as to obtain them from there, and I'm using a hashmap (mapa) and this method to get them:

public static String consiguePath (int i) {


return AppUtils.class.getClass().getResource("/Movimiento/" + mapa.get(i)).getPath();

}

so as to avoid absolute paths.

When I open an MP3 file I do this:

try {
                            File archivo = new File(AppUtils.consiguePath(12));
                            FileInputStream fis = new FileInputStream(archivo);
                            BufferedInputStream bis = new BufferedInputStream(fis);
                            try {
                                Player player = new Player(bis);
                                player.play();
                            } catch (JavaLayerException jle) {
                            }
                        } catch (IOException e) {
                        }

The whole thing runs perfectly in NetBeans, but when I build a .jar file and execute it it runs well but it won't open the MP3 files. What called my attention is that it doesn't have trouble in opening the JPG files that are on the same package.

After generating the .jar I checked the MyProject/build/classes/Movimiento folder and all of the MP3 files were actually there, so I don't know what may be happening.

I've seen others had this problem before but I haven't seen any satisfactory answer yet.

Thanks!

4

1 回答 1

0

更改以consiguePath返回结果URLgetResource

public static URL consiguePath(int i) {

    return AppUtils.class.getClass().getResource("/Movimiento/" + mapa.get(i));

}

然后用它InputStream传递给Player

try {
    URL url = AppUtils.consiguePath(12);
    Player player = new Player(url.openStream());
    player.play();
} catch (JavaLayerException | IOException e) {
    e.printStackTrace();
}

同样,您可以只使用Class#getResourceAsStream

资源被打包到你的Jar文件中,不能再被当作Files

于 2015-08-05T06:40:17.743 回答