簡體   English   中英

javafx 2 MediaException: MEDIA_UNAVAILABLE 不會加載文件

[英]javafx 2 MediaException: MEDIA_UNAVAILABLE won't load file

我正在嘗試在 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();

此外,在您的 src 中保留一個Resources文件夾並在其中隔離文件也是一個好主意。 像這樣的東西:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM