📅  最后修改于: 2020-10-14 07:28:08             🧑  作者: Mango
用JavaFX播放视频非常简单。我们需要使用与播放音频文件时相同的API。在播放视频的情况下,我们需要使用MediaView节点将视频显示到场景中。
为此,我们需要通过将Mediaplayer对象传递给其构造函数来实例化MediaView类。由于MediaView是JavaFX节点,因此我们可以对其应用效果。
在本教程的这一部分中,我们将讨论播放视频媒体文件所涉及的步骤以及与此有关的一些示例。
package application;
import java.io.File;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import javafx.stage.Stage;
public class JavaFX_Media Example extends Application
{
@Override
publicvoid start(Stage primaryStage) throws Exception {
// TODO Auto-generated method stub
//Initialising path of the media file, replace this with your file path
String path = "/home/javatpoint/Downloads/test.mp4";
//Instantiating Media class
Media media = new Media(new File(path).toURI().toString());
//Instantiating MediaPlayer class
MediaPlayer mediaPlayer = new MediaPlayer(media);
//Instantiating MediaView class
MediaView mediaView = new MediaView(mediaPlayer);
//by setting this property to true, the Video will be played
mediaPlayer.setAutoPlay(true);
//setting group and scene
Group root = new Group();
root.getChildren().add(mediaView);
Scene scene = new Scene(root,500,400);
primaryStage.setScene(scene);
primaryStage.setTitle("Playing video");
primaryStage.show();
}
publicstaticvoid main(String[] args) {
launch(args);
}
}