javafx 2 媒体异常 : MEDIA_UNAVAILABLE won't load file

JeffreyZhao 阅读:23 2024-12-31 21:38:35 评论:0

我正在尝试在 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
我在这里遗漏了什么明显的东西吗?

请您参考如下方法:

使用文件路径时,最好使用 getClass().getResource("/PATH/RELATIVE/TO/SRC").toExternalForm()
在这种情况下,您的代码将必须如下所示:

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

此外,最好保留 Resources src 中的文件夹并隔离其中的文件。像这样的东西:
├───src/ 
│   ├───Other Packages/ 
│   ├───Resources/ 
│   │   ├───Sound/ 
.   .   . 
.   .   .  


标签:java
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

关注我们

一个IT知识分享的公众号