如何在Java/JavaFX中定位文件(指向文件的路径) [英] How to target a file (a path to it) in Java/JavaFX
问题描述
这可能很简单,但我似乎无法使其正常工作.我正在用JavaFX制作视频播放器,但是我不知道如何定位将要播放的文件(我不知道正确的语法).预先感谢您的帮助.这是我尝试运行的代码示例>
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);
顺便说一句,该文件位于项目文件夹中,然后是trailers/trailer.mp4.哦,我正在运行Windows.
btw, the file is in the project folder, then trailers/trailer.mp4. Oh, and I'm running Windows.
推荐答案
将文件放入sources文件夹并将其作为资源加载:
Put your file into the sources folder and load it as a resource:
Media media = new Media(getClass().getResource("trailer.mp4"));
或使用完整路径
Media media = new Media("file://c:/trailers/trailer.mp4"));
此外,请注意JavaFX 2.0仅支持FLV编解码器.对于mp4(带有H.264编解码器),您需要使用JavaFX 2.1或更高版本.
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.
这篇关于如何在Java/JavaFX中定位文件(指向文件的路径)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!