MediaView
MediaView is used to display video in JavaFX. It works together with Media and MediaPlayer.
Media= the video fileMediaPlayer= controls play, pause, stopMediaView= shows the video on the screen
Example: Simple Video Player
This example can:
- Load a video automatically
- Play, pause, stop
- Show the current status
FXML
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.media.MediaView?>
<VBox spacing="15"
alignment="CENTER"
xmlns="http://javafx.com/javafx/17"
xmlns:fx="http://javafx.com/fxml/1"
fx:controller="org.example.demo.HelloController">
<Label text="Video Player"
style="-fx-font-size: 20px; -fx-font-weight: bold;"/>
<MediaView fx:id="mediaView"
fitWidth="600"
fitHeight="350"
preserveRatio="true"/>
<HBox spacing="10" alignment="CENTER">
<Button text="Play"
onAction="#playVideo"/>
<Button text="Pause"
onAction="#pauseVideo"/>
<Button text="Stop"
onAction="#stopVideo"/>
</HBox>
<Label fx:id="statusLabel"
text="Video not loaded yet."/>
</VBox>
Controller
package org.example.demo;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
public class HelloController {
@FXML
private MediaView mediaView;
@FXML
private Label statusLabel;
private MediaPlayer mediaPlayer;
@FXML
private void initialize() {
String videoPath = getClass()
.getResource("/videos/sample.mp4")
.toExternalForm();
Media media = new Media(videoPath);
mediaPlayer = new MediaPlayer(media);
mediaView.setMediaPlayer(mediaPlayer);
statusLabel.setText("Video loaded successfully.");
}
@FXML
private void playVideo() {
mediaPlayer.play();
statusLabel.setText("Video is playing.");
}
@FXML
private void pauseVideo() {
mediaPlayer.pause();
statusLabel.setText("Video paused.");
}
@FXML
private void stopVideo() {
mediaPlayer.stop();
statusLabel.setText("Video stopped.");
}
}
Project structure
src
└── main
└── resources
└── videos
└── sample.mp4
Required module
MediaView needs the JavaFX media module.
In module-info.java:
module org.example.demo {
requires javafx.controls;
requires javafx.fxml;
requires javafx.media;
opens org.example.demo to javafx.fxml;
exports org.example.demo;
}
What happens?
When the program starts:
Media media = new Media(videoPath);
mediaPlayer = new MediaPlayer(media);
mediaView.setMediaPlayer(mediaPlayer);
The video is prepared, but not playing yet.
When the user clicks "Play":
mediaPlayer.play();
The video starts.
When the user clicks "Pause":
mediaPlayer.pause();
The video pauses.
When the user clicks "Stop":
mediaPlayer.stop();
The video stops and returns to the beginning.
Longer example: automatically play when loaded
@FXML
private void initialize() {
String videoPath = getClass()
.getResource("/videos/sample.mp4")
.toExternalForm();
Media media = new Media(videoPath);
mediaPlayer = new MediaPlayer(media);
mediaPlayer.setOnReady(() -> {
statusLabel.setText("Video duration: "
+ media.getDuration().toSeconds() + " seconds");
mediaPlayer.play();
});
mediaPlayer.setOnEndOfMedia(() -> {
statusLabel.setText("The video finished.");
});
mediaView.setMediaPlayer(mediaPlayer);
}
Useful methods
mediaPlayer.play();
mediaPlayer.pause();
mediaPlayer.stop();
mediaPlayer.setVolume(0.5); // 50% volume
mediaPlayer.setRate(2.0); // double speed
mediaPlayer.seek(Duration.seconds(30)); // jump to 30 seconds
Do not forget:
import javafx.util.Duration;
Example:
mediaPlayer.seek(Duration.seconds(10));
This moves the video to second 10.