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

随声附和 提交于 2019-11-27 23:22:40

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.

1 Use this if media source file in same project package.

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

2 Use this if media source file in same project sub package [Packages with name "trailers" in project main package]

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

3 Use this if media source file is other location [Using full path].

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

OR

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

Note: should use 3 slash i.e. "file:///" to avoid error "MediaException: MEDIA_INACCESSIBLE : e/E"

If you want to load media from your project package:

File file=new File("trailer.mp4");
Media media=new Media(file.toURI().toString())
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!