【问题标题】:javafx 2 MediaException: MEDIA_UNAVAILABLE won't load filejavafx 2 MediaException:MEDIA_UNAVAILABLE 不会加载文件
【发布时间】:2013-06-26 12:17:32
【问题描述】:

我正在尝试在 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】:

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

在这种情况下,您的代码将必须如下所示:

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

另外,在您的 src 中保留一个 Resources 文件夹并在其中隔离文件是一个好主意。像这样的:

├───src/
│   ├───Other Packages/
│   ├───Resources/
│   │   ├───Sound/
.   .   .
.   .   . 

【讨论】:

  • 相对路径(在 class.getResource 中用作参数时)不能以“/”开头 - 请阅读 api 文档
  • @kleopatra 嗨!感谢您的反馈:) 也许我误解了doc's。它声明“如果名称以'/'('\u002f')开头,那么资源的绝对名称是名称中'/'之后的部分。”我在我的几个项目中使用过它,并且它有效,没有它我通常会出错。
猜你喜欢
  • 2020-04-09
  • 2016-09-20
  • 2017-04-17
  • 1970-01-01
  • 2020-02-01
  • 2013-10-05
  • 2014-07-04
  • 2011-10-14
  • 1970-01-01
相关资源
最近更新 更多