- JavaFX Tutorial
- JavaFX - Home
- JavaFX - Overview
- JavaFX Installation and Architecture
- JavaFX - Installation Using Netbeans
- JavaFX - Installation Using Eclipse
- JavaFX - Installation using Visual Studio Code
- JavaFX - Architecture
- JavaFX - Application
- JavaFX 2D Shapes
- JavaFX - 2D Shapes
- JavaFX - Drawing a Line
- JavaFX - Drawing a Rectangle
- JavaFX - Drawing a Rounded Rectangle
- JavaFX - Drawing a Circle
- JavaFX - Drawing an Ellipse
- JavaFX - Drawing a Polygon
- JavaFX - Drawing a Polyline
- JavaFX - Drawing a Cubic Curve
- JavaFX - Drawing a Quad Curve
- JavaFX - Drawing an Arc
- JavaFX - Drawing an SVGPath
- JavaFX Properties of 2D Objects
- JavaFX - Stroke Type Property
- JavaFX - Stroke Width Property
- JavaFX - Stroke Fill Property
- JavaFX - Stroke Property
- JavaFX - Stroke Line Join Property
- JavaFX - Stroke Miter Limit Property
- JavaFX - Stroke Line Cap Property
- JavaFX - Smooth Property
- Operations on 2D Objects
- JavaFX - 2D Shapes Operations
- JavaFX - Union Operation
- JavaFX - Intersection Operation
- JavaFX - Subtraction Operation
- JavaFX Color and Texture
- JavaFX - Colors
- JavaFX Text
- JavaFX - Text
- JavaFX Effects
- JavaFX - Effects
- JavaFX Transformations
- JavaFX - Transformations
- JavaFX Animations
- JavaFX - Animations
- JavaFX Images
- JavaFX - Images
- JavaFX 3D Shapes
- JavaFX - 3D Shapes
- JavaFX - Creating a Box
- JavaFX - Creating a Cylinder
- JavaFX - Creating a Sphere
- Properties of 3D Objects
- JavaFX - Cull Face Property
- JavaFX - Drawing Modes Property
- JavaFX - Material Property
- JavaFX Event Handling
- JavaFX - Event Handling
- JavaFX - Using Convenience Methods
- JavaFX - Event Filters
- JavaFX - Event Handlers
- JavaFX UI Controls
- JavaFX - UI Controls
- JavaFX - ListView
- JavaFX - Accordion
- JavaFX - ButtonBar
- JavaFX - ChoiceBox
- JavaFX - HTMLEditor
- JavaFX - MenuBar
- JavaFX - Pagination
- JavaFX - ProgressIndicator
- JavaFX - ScrollPane
- JavaFX - Separator
- JavaFX - Slider
- JavaFX - Spinner
- JavaFX - SplitPane
- JavaFX - TableView
- JavaFX - TabPane
- JavaFX - ToolBar
- JavaFX - TreeView
- JavaFX - Label
- JavaFX - CheckBox
- JavaFX - RadioButton
- JavaFX - TextField
- JavaFX - PasswordField
- JavaFX - FileChooser
- JavaFX - Hyperlink
- JavaFX - Tooltip
- JavaFX - Alert
- JavaFX - DatePicker
- JavaFX - TextArea
- JavaFX Charts
- JavaFX - Charts
- JavaFX - Creating Pie Chart
- JavaFX - Creating Line Chart
- JavaFX - Creating Area Chart
- JavaFX - Creating Bar Chart
- JavaFX - Creating Bubble Chart
- JavaFX - Creating Scatter Chart
- JavaFX - Creating Stacked Area Chart
- JavaFX - Creating Stacked Bar Chart
- JavaFX Layout Panes
- JavaFX - Layout Panes
- JavaFX - HBox Layout
- JavaFX - VBox Layout
- JavaFX - BorderPane Layout
- JavaFX - StackPane Layout
- JavaFX - TextFlow Layout
- JavaFX - AnchorPane Layout
- JavaFX - TilePane Layout
- JavaFX - GridPane Layout
- JavaFX - FlowPane Layout
- JavaFX CSS
- JavaFX - CSS
- Media with JavaFX
- JavaFX - Playing Video
- JavaFX Useful Resources
- JavaFX - Quick Guide
- JavaFX - Useful Resources
- JavaFX - Discussion
JavaFX - Playing Video
Video is a medium of visual communication. In our daily lives, we can observe its applications in various sectors such as the entertainment industry, news agencies, and educational platforms and many more. For example, we can see the YouTube video player in the below figure −
Playing Video in JavaFX
To play videos in JavaFX, we need to embed it within the application. There are different video formats available, out of which JavaFX supports only two namely MPEG-4(in short mp4) and FLV. Follow the steps given below to embed a video in JavaFX application −
First, instantiate Media class of the package named javafx.scene.media by passing path of the video file.
Next, create a MediaPlayer object by passing the Media object as a parameter value to its constructor.It will enable the media to be played in the JavaFX application.
Then, instantiate MediaView class by passing the MediaPlayer object to its constructor as a parameter value. Doing so will allow JavaFX application to display the video.
Lastly, create any layout pane and set the scene and stage with the desired dimension.
Example
The following JavaFX program demonstrates how to embed a Video into a JavaFX application. Save this code in a file with the name Javafx_Video.java.
import java.io.File; import javafx.application.Application; import javafx.geometry.Pos; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.layout.HBox; import javafx.scene.layout.VBox; import javafx.scene.media.Media; import javafx.scene.media.MediaPlayer; import javafx.scene.media.MediaView; import javafx.stage.Stage; public class Javafx_Video extends Application { @Override public void start(Stage stage) { // Passing the video file to the File object File videofile = new File("sampleTP.mp4"); // creating a Media object from the File Object Media videomedia = new Media(videofile.toURI().toString()); // creating a MediaPlayer object from the Media Object MediaPlayer mdplayer = new MediaPlayer(videomedia); // creating a MediaView object from the MediaPlayer Object MediaView viewmedia = new MediaView(mdplayer); //setting the fit height and width of the media view viewmedia.setFitHeight(455); viewmedia.setFitWidth(500); // creating video controls using the buttons Button pause = new Button("Pause"); Button resume = new Button("Resume"); // creating an HBox HBox box = new HBox(20, pause, resume); box.setAlignment(Pos.CENTER); // function to handle play and pause buttons pause.setOnAction(act -> mdplayer.pause()); resume.setOnAction(act -> mdplayer.play()); // creating the root VBox root = new VBox(20); root.setAlignment(Pos.CENTER); root.getChildren().addAll(viewmedia, box); Scene scene = new Scene(root, 400, 400); stage.setScene(scene); stage.setTitle("Example of Video in JavaFX"); stage.show(); } public static void main(String[] args) { launch(args); } }
To compile and execute the saved Java file from the command prompt, use the following commands −
javac --module-path %PATH_TO_FX% --add-modules javafx.controls,javafx.media Javafx_Video.java java --module-path %PATH_TO_FX% --add-modules javafx.controls,javafx.media Javafx_Video
Output
When we execute the above code, it will generate the following output.