How to target a file (a path to it) in Java/JavaFX

file, javafx-2, media, path

Solution

Put your file into the sources folder and load it as a resource:

Media media = new Media(getClass().getResource("trailer.mp4"));

or use the full path

Media media = new Media("file://c:/trailers/trailer.mp4"));

Also, note that JavaFX 2.0 supports only FLV codec. For mp4 (with H.264 codec) you need to use JavaFX 2.1 or later.

Problem

It might be a simple one, but i can't seem to get it to work. I am making a video player in JavaFX but I don't know how to target the file that is going to be played (I don't know the correct syntax). Thank you in advance for your help. Here's a sample of code that i'm trying to run> ``` Media media = new Media("trailers/trailer.mp4"); MediaPlayer player = new MediaPlayer(media); MediaView view = new MediaView(player); ``` btw, the file is in the project folder, then trailers/trailer.mp4. Oh, and I'm running Windows.

Original source