0

I have a problem loading my mp3 files in jar. I am trying to load my mp3 to my jar. When I use the code

new Media(new File(Audio.class.getResource(path).toURI()).toURI().toString())

I get an exception says the URI isn't hierarchical, so I search for another solution for that. What I found is using inputstream to load it, and I tried using

new Media(new File(Audio.class.getResourceAsStream(path).toString()).toURI().toString())

I found out that the jar can find my mp3 file but the but it still give an exception

MEDIA_UNAVAILABLE : C:\Users\Brian\Desktop\Boring Fighting Game\Boring Fighting Game\java.io.BufferedInputStream@6d86b085 (The system cannot find the file specified)
    at javafx.scene.media.Media.<init>(Media.java:407)
    at Something.Audio.load(Audio.java:25)
    at Something.BGM.<init>(BGM.java:16)
    at MainGame.GameController.init(GameController.java:24)
    at MainGame.GameController.<init>(GameController.java:19)
    at MainGame.Handler.<init>(Handler.java:24)
    at MainGame.Game.<init>(Game.java:38)
    at Lancher.Launcher.main(Launcher.java:11)

I'm trying to solve this problem but just can't. Can anyone give me an answer or other solution for this?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • note: your question had two `toUri()`, probably that what breaks the code. – Bagus Tesa Aug 26 '18 at 12:03
  • Application resources will become embedded resources by the time of deployment, so it is wise to start accessing them as if they were, right now. **An [tag:embedded-resource] must be accessed by URL *rather than* file.** See the [info. page for embedded resource](http://stackoverflow.com/tags/embedded-resource/info) for how to form the URL. – Andrew Thompson Aug 26 '18 at 13:49
  • .. as such, this might work: `new Media( Audio.class.getResource(path).toURI()).toURI().toString())` – Andrew Thompson Aug 26 '18 at 13:50

0 Answers0