2017-08-05 131 views
0

我試圖在簡單的javafx應用程序中播放視頻(格式無關緊要 - 我已將其轉換爲flv,mov和mp4)。我只想在窗口中播放視頻。如何在Javafx中播放視頻文件? - >示例代碼

下面的示例代碼已正常工作 - 但只能使用來自oracle網站的URL視頻。即使我給媒體對象一個包含位置的文件,我也無法使其與我的計算機上的本地文件一起工作。

如何修改代碼以在我的項目文件夾中播放文件?

Here's代碼:

package Video; 

import javafx.application.Application; 
import javafx.scene.Group; 
import javafx.scene.Scene; 
import javafx.scene.layout.StackPane; 
import javafx.scene.media.Media; 
import javafx.scene.media.MediaPlayer; 
import javafx.scene.media.MediaView; 
import javafx.stage.Stage; 

public class player extends Application{ 

StackPane stack = new StackPane(); 



public void start(Stage primaryStage) throws Exception { 

Media media = new 
Media("http://download.oracle.com/otndocs/products/javafx/oow2010- 
2.flv"); 

MediaPlayer player = new MediaPlayer(media); 


primaryStage.setScene(new Scene(new Group(new MediaView(player)), 540, 
208)); 
primaryStage.setTitle("Title"); 
primaryStage.show(); 

player.play(); 
} 

public static void main(String [] args) { 
    Application.launch(); 
} 

} 

回答

0
//declare the media player and file to have class scope 
final File file = new File("x.mp4"); 
    final String MEDIA_URL = file.toURI().toString(); 

    // Create a Media 
    final Media media = new Media(MEDIA_URL); 

    // Create a Media Player 
    final MediaPlayer player = new MediaPlayer(media); 

這將尋找在同一個目錄中你的java文件的文件。