如何在 JavaFX 中访问我的计算机上的本地媒体文件?

发布于 2024-09-04 21:44:59 字数 349 浏览 0 评论 0原文

如何在 JavaFX 中访问我的计算机上的本地媒体文件?

以下是我尝试过的网址:

C:/PROJECT/videos/on2tm_352.flv
file:///C://PROJECT/videos/on2tm_352.flv (在某些站点中建议忘记在哪里)

但是,当我将媒体文件放入项目的文件夹中并使用它访问它时,它确实会播放{__DIR__}/on2tm_352.flv

注意:没有输出任何异常和错误。屏幕一片空白。

使用 KLite Codec 583 Mega、JavaFX 1.2、Netbeans 6.8

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

燃情 2024-09-11 21:44:59

它现在对我来说正在工作:

private static final String MEDIA_URL = "file:/c:/Users/Alejandro/Downloads/oow2010-2.flv";

我几分钟前测试过......

或者类似的东西:

private File file = new File("c:/Users/Alejandro/Downloads/oow2010-2.flv");
private final String MEDIA_URL = file.toURI().toString();

稍后见=D

It is working right now for me:

private static final String MEDIA_URL = "file:/c:/Users/Alejandro/Downloads/oow2010-2.flv";

I tested it a few minutes ago....

or something like that:

private File file = new File("c:/Users/Alejandro/Downloads/oow2010-2.flv");
private final String MEDIA_URL = file.toURI().toString();

See you later =D

暖伴 2024-09-11 21:44:59

试试这个:

Media media = new Media(trackFile.toURI().toURL().toString());
MediaPlayer mediaPlayer = new MediaPlayer(media);

Try this out:

Media media = new Media(trackFile.toURI().toURL().toString());
MediaPlayer mediaPlayer = new MediaPlayer(media);
北方的巷 2024-09-11 21:44:59

通过向媒体提供他们的网址?

请注意,过去曾报告过一些路径中空格的问题,我不知道是否仍然如此。

[在原始消息编辑后进行编辑(URL 示例)]
第一行不是 URL,而是路径。显然,媒体播放器接受路径作为 URL,但 ImageView 的情况并非如此,因此最好严格一些。
第二行是正确的。
第三行有一个潜在的问题:__DIR__变量已经有一个终端斜杠,所以你不应该添加它,即。改为编写 {__DIR__}on2tm_352.flv 。不确定这是否是问题所在(我还没有使用太多视频),但值得尝试。

请注意,一旦项目打包,此类 URL(基于 __DIR__ )将指向 jar 文件内部。
在 JavaFX 1.2 中这是可以的,但由于某些奇怪的原因,他们选择在 1.3 中禁止此类访问。

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.

二货你真萌 2024-09-11 21:44:59

我发现使用磁盘文件执行以下操作更容易。这让我的弱智不再需要确定“file:”url 的所有规则:

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

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

我避免对媒体使用 {__DIR__} ,因为它可以指向“jar:”URL,并且不再支持该 URL。 JavaFX 1.3 中的媒体位置。

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.

梦太阳 2024-09-11 21:44:59

你们只需将文件的路径指定为 URI 路径即可:

Media media = new Media("file:///C:/Users/David/Downloads/test.flv");
MediaPlayer mediaPlayer = new MediaPlayer(media);

根本不需要实例化文件。

You guys just have to specify the file's path as an URI path:

Media media = new Media("file:///C:/Users/David/Downloads/test.flv");
MediaPlayer mediaPlayer = new MediaPlayer(media);

It's not required to instantiate a File at all.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文