javafx 2 MediaException:MEDIA_UNAVAILABLE无法加载文件

问题描述 投票:5回答:1

我正在尝试在javafx2中运行flv文件。我的代码如下:

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();

我收到以下错误:

Caused by: MediaException: MEDIA_UNAVAILABLE : C:\Users\Darren\workspace\player\src\player\football.flv (The system cannot find the file specified)

我已经在这里查看过类似的帖子。我还尝试过将视频文件存储在不同的位置,并尝试了以各种方式访问​​它的负载。

文件的路径是:

C:\Users\Darren\workspace\player\src\player

我在这里错过任何明显的东西吗?

java media-player javafx-2 javafx
1个回答
0
投票

[使用文件路径时,最好使用getClass().getResource("/PATH/RELATIVE/TO/SRC").toExternalForm()

在这种情况下,您的代码将必须看起来像这样:

Media media = new Media(getClass().getResource("/player/football.flv").toExternalForm());
MediaPlayer palyer = new MediaPlayer(media);
player.play();

此外,最好在src中保留一个Resources文件夹并分隔其中的文件。像这样的东西:

├───src/
│   ├───Other Packages/
│   ├───Resources/
│   │   ├───Sound/
.   .   .
.   .   . 
© www.soinside.com 2019 - 2024. All rights reserved.