[英]javafx 2 MediaException: MEDIA_UNAVAILABLE won't load file
I'm trying to run an flv file in javafx2.我正在尝试在 javafx2 中运行一个 flv 文件。 My code is the following:
我的代码如下:
Media media = new Media("file:///C:/Users/Darren/workspace/player/src/player/football.flv");
MediaPlayer player = new MediaPlayer(media);
MediaView view = new MediaView(player);
root.getChildren().add(view);
Scene scene = new Scene(root, 400, 400, Color.BLACK);
stage.setScene(scene);
stage.show();
player.play();
I get the following error:我收到以下错误:
Caused by: MediaException: MEDIA_UNAVAILABLE : C:\Users\Darren\workspace\player\src\player\football.flv (The system cannot find the file specified)
I have checked out similar posts on here.我在这里查看了类似的帖子。 I have also tried storing the video file in different places and tried loads of different ways of accessing it.
我还尝试将视频文件存储在不同的地方,并尝试了多种不同的访问方式。
The path to the file is:文件路径为:
C:\\Users\\Darren\\workspace\\player\\src\\player
Am I missing anything obvious here?我在这里遗漏了什么明显的东西吗?
When working with file paths it is always a good idea to use getClass().getResource("/PATH/RELATIVE/TO/SRC").toExternalForm()
处理文件路径时,最好使用
getClass().getResource("/PATH/RELATIVE/TO/SRC").toExternalForm()
In that case your code is going to have to look like this:在这种情况下,您的代码将必须如下所示:
Media media = new Media(getClass().getResource("/player/football.flv").toExternalForm());
MediaPlayer player = new MediaPlayer(media);
player.play();
Also it is a good idea to keep a Resources
folder within you src and segregate files within it.此外,在您的 src 中保留一个
Resources
文件夹并在其中隔离文件也是一个好主意。 Something like this:像这样的东西:
├───src/
│ ├───Other Packages/
│ ├───Resources/
│ │ ├───Sound/
. . .
. . .
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.