6

There is only one constructor of the Media-class: public Media(java.lang.String source) see http://docs.oracle.com/javafx/2/api/javafx/scene/media/Media.html#Media%28java.lang.String%29

This constructor gets a URI as string. I have a JavaFX project and put a WAV file inside this project. When I deploy the project as a JAR, I can see (with 7-Zip for example), that the WAV file is also exported. There is no problem to get the content with

MyApplicationClass.class.getResourceAsStream("/resources/test.wav").

But what is the correct URI to refer this WAV file inside the deployed JAR for the Media constructor? The URI

new Media("jar:.!/resources/test.wav")

doesnt work. The URI "jar:resources/test.wav" fails too (becouse there is no reference to the JAR file).

Does anybody have an idea about the correct URI?

Vertex
  • 2,682
  • 3
  • 29
  • 43

1 Answers1

7

try this one out

new Media(MyApplicationClass.class.getResource("/resources/test.wav").toURI().toString())

Media accepts jar uris so it should work fine

Ion Cojocaru
  • 2,583
  • 15
  • 16
  • 1
    Thank you! `ViMu.class.getResource("/audio/click_track_a.wav").toURI().toString()` gives a correct URI `jar:file:/C:Users/oliver/Desktop/vimu.jar!audio/click_track_a.wav` and the `Media` object created correctly becouse there is no `MEDIA UNAVAILABLE`. But by playing it with `MediaPlayer` I can't here it. With the URI `file:/D:/Workspace/vimu/bin/audio/click_track_a.wav` I can here it. – Vertex Mar 27 '13 at 15:27
  • this is using JMF: URL url = ViMu.class.getResource("/audio/click_track_a.wav"); URLDataSource uds = new URLDataSource(url); uds.connect(); Player player = Manager.createPlayer(uds); http://stackoverflow.com/questions/8304940/how-to-load-media-resources-from-the-classpath-in-jmf – Ion Cojocaru Mar 27 '13 at 15:46
  • Ok, thank you. I will consider JMF for it and hope, that JavaFX `Media` will accept arbitary `InputStream`s in the future. – Vertex Mar 27 '13 at 17:43
  • 3
    Ahh, the problem is the length of the WAV file. I've tested with 1 s WAV and it works. MediaPlayer is very buggy :) – Vertex Mar 28 '13 at 11:06
  • this does not work for valid MP4 video resources (using 1.8.0_51). ~25s video will play once and then freeze. when i copy the jar URL to a temporary file and use that as the source, it will loop. @Vertex yes, JavaFX is very buggy in general. – mre Jun 11 '18 at 21:31