如何在 Java/JavaFX 中定位文件(它的路径) [英] How to target a file (a path to it) in Java/JavaFX

查看:33
本文介绍了如何在 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);

顺便说一句,文件在项目文件夹中,然后是拖车/拖车.mp4.哦,我运行的是 Windows.

btw, the file is in the project folder, then trailers/trailer.mp4. Oh, and I'm running Windows.

推荐答案

将你的文件放入源文件夹并作为资源加载:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆