tags:

views:

218

answers:

2

How to access local media file on my computer in JavaFX?

Here are the urls I tried:

C:/PROJECT/videos/on2tm_352.flv
file:///C://PROJECT/videos/on2tm_352.flv (suggested in some site forgot where)

It does play however, when I put the media file inside the project's folder and access it using {__DIR__}/on2tm_352.flv

Note: There are no exceptions and errors outputted. The screen is just blank.

KLite Codec 583 Mega, JavaFX 1.2, Netbeans 6.8 are used

A: 

By giving their URL to the Media?

Note that some issues with spaces in paths have been reported in the past, I don't know if it is still true.

[EDIT following original message edit (URL examples)]
First line isn't an URL, it is a path. Apparently the media player accepts paths as URL, but that's not the case for ImageView, though, so it is better to be strict.
Second line is correct.
Third line have a potential issue: __DIR__ variables has already a terminal slash, so you should not add it, ie. write {__DIR__}on2tm_352.flv instead. Not sure if that's the issue (I haven't used much video yet) but worth trying.

Note that such URL (based on __DIR__) will point inside a jar file once the project is packaged. It is OK in JavaFX 1.2, but for some odd reason, they chose to disallow such access in 1.3.

PhiLho
The url I supplied doesn't have spaces. Still it wouldn't work
cancelledout
@cancelledout: What URL? As long as you remain vague, answers will be generic... Do you have an error? Is your media file in a supported format?
PhiLho
Here it is:I tried:C:/PROJECT/videos/on2tm_352.flvfile:///C://PROJECT/videos/on2tm_352.flv (suggested in some site forgot where)IT does play however, when I put the media file inside the project's folder and access it using {__DIR__}/on2tm_352.flvThere are no exceptions and errors outputted. The screen is just blank.KLite Codec 583 Mega, JavaFX 1.2, Netbeans 6.8 are used.
cancelledout
@cancelledout: see my edit for more information
PhiLho
I still can't access the local file via the URL. file:///C:/... =X
cancelledout
TIP: What I usually do is create a java.io.File object with the path to the local file, then use "{File.toURI()}" as the location for the Media. Saves a lot of headaches trying to get the "file:" URL right. Also note, JavaFX 1.3 no longer allows media files to be loaded from jar files, so if your class is in a jar file, the "{__DIR__}" will not work because it will be a "jar://" url.
JimClarke
A: 

I have found it easier to do the following with disk files. This relieves my feeble brain of determining all the rules for "file:" urls:

var file = new File("C:/PROJECT/videos/on2tm_352.flv");

Media {
   source: "{file.toURI()}"
}

I avoid using {__DIR__} for media as it can point to a "jar:" URL and that is no longer supported for media locations in JavaFX 1.3.

JimClarke
Thank you for the answer. So if {__DIR__} is not supported, how can I access those files in a jar then?
cancelledout
Just to be clear {__DIR__} is supported. What is not supported is loading a media file from a Jar file. So, if {__DIR__} points to a jar file, the media will not load. For an explanation see http://www.javafx.com/faq/#5.3.
JimClarke