Open In App

JavaFX | Building a Media Player

Last Updated : 19 Sep, 2023
Like Article

This JavaFX library is used to make feature-rich internet apps (they offer similar experience and features as that of desktop apps). Like other Java libraries, the products built upon this library are platform independent, they can run on devices such as mobile phones, TVs, computers etc.

Other JVM based technologies like Groovy, JRuby etc can be used alongside JavaFX, however, the need seldom arises as JavaFX is offering most of the features itself. It is highly compatible with Java Swing, and its content can be embedded seamlessly in JavaFX apps.

Building a Media Player in JavaFX

For the media player application we would have three different classes the first one is our Main class which starts this application, then we have Player class to run our videos and audios and MediaBar class to control our media.


// Java program to Build a Media 
// Player in JavaFX
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
// launches the application
public class Main extends Application { 
    Player player;
    FileChooser fileChooser;
    public void start(final Stage primaryStage)
        // setting up the stages
        MenuItem open = new MenuItem("Open");
        Menu file = new Menu("File");
        MenuBar menu = new MenuBar();
        // Connecting the above three
        file.getItems().add(open); // it would connect open with file
        // Adding functionality to switch to different videos
        fileChooser = new FileChooser();
        open.setOnAction(new EventHandler<ActionEvent>(){
            public void handle(ActionEvent e)
                // Pausing the video while switching
                File file = fileChooser.showOpenDialog(primaryStage);
                // Choosing the file to play
                if (file != null) {
                    try {
                        player = new Player(file.toURI().toURL().toExternalForm());
                        Scene scene = new Scene(player, 720, 535, Color.BLACK);
                    catch (MalformedURLException e1) {
            // here you can choose any video
            player = new Player("file:/// F:/songs/srk.mp4");
            // Setting the menu at the top
            // Adding player to the Scene
            Scene scene = new Scene(player, 720, 535, Color.BLACK); 
            // height and width of the video player
            // background color set to Black
            primaryStage.setScene(scene); // Setting the scene to stage
  ; // Showing the stage
     // Main function to launch the application
     public static void main(String[] args){
    import javafx.scene.layout.BorderPane;
    import javafx.scene.layout.Pane;
    public class Player extends BorderPane // Player class extend BorderPane
    // in order to divide the media
    // player into regions
        Media media;
        MediaPlayer player;
        MediaView view;
        Pane mpane;
        MediaBar bar;
        public Player(String file)
        { // Default constructor
            media = new Media(file);
            player = new MediaPlayer(media);
            view = new MediaView(player);
            mpane = new Pane();
            mpane.getChildren().add(view); // Calling the function getChildren
            // inorder to add the view
            bar = new MediaBar(player); // Passing the player to MediaBar
            setBottom(bar); // Setting the MediaBar at bottom
            setStyle("-fx-background-color:#bfc2c7"); // Adding color to the mediabar
  ; // Making the video play
    import javafx.application.Platform;
    import javafx.beans.InvalidationListener;
    import javafx.beans.Observable;
    import javafx.event.ActionEvent;
    import javafx.event.EventHandler;
    import javafx.geometry.Insets;
    import javafx.geometry.Pos;
    import javafx.scene.control.Button;
    import javafx.scene.control.Label;
    import javafx.scene.control.Slider;
    import javafx.scene.layout.HBox;
    import javafx.scene.layout.Priority;
    public class MediaBar extends HBox { // MediaBar extends Horizontal Box
        // introducing Sliders
        Slider time = new Slider(); // Slider for time
        Slider vol = new Slider(); // Slider for volume
        Button PlayButton = new Button("||"); // For pausing the player
        Label volume = new Label("Volume: ");
        MediaPlayer player;
        public MediaBar(MediaPlayer play)
        { // Default constructor taking
            // the MediaPlayer object
            player = play;
            setAlignment(Pos.CENTER); // setting the HBox to center
            setPadding(new Insets(5, 10, 5, 10));
            // Settih the preference for volume bar
            HBox.setHgrow(time, Priority.ALWAYS);
            // Adding the components to the bottom
            getChildren().add(PlayButton); // Playbutton
            getChildren().add(time); // time slider
            getChildren().add(volume); // volume slider
            // Adding Functionality
            // to play the media player
            PlayButton.setOnAction(new EventHandler<ActionEvent>() {
                public void handle(ActionEvent e)
                    Status status = player.getStatus(); // To get the status of Player
                    if (status == status.PLAYING) {
                        // If the status is Video playing
                        if (player.getCurrentTime().greaterThanOrEqualTo(player.getTotalDuration())) {
                            // If the player is at the end of video
                  ; // Restart the video
                        else {
                            // Pausing the player
                    } // If the video is stopped, halted or paused
                    if (status == Status.HALTED || status == Status.STOPPED || status == Status.PAUSED) {
              ; // Start the video
            // Providing functionality to time slider
            player.currentTimeProperty().addListener(new InvalidationListener() {
                public void invalidated(Observable ov)
            // Inorder to jump to the certain part of video
            time.valueProperty().addListener(new InvalidationListener() {
                public void invalidated(Observable ov)
                    if (time.isPressed()) { // It would set the time
                        // as specified by user by pressing
               / 100));
            // providing functionality to volume slider
            vol.valueProperty().addListener(new InvalidationListener() {
                public void invalidated(Observable ov)
                    if (vol.isPressed()) {
                        player.setVolume(vol.getValue() / 100); // It would set the volume
                        // as specified by user by pressing
        // Outside the constructor
        protected void updatesValues()
            Platform.runLater(new Runnable() {
                public void run()
                    // Updating to the new time value
                    // This will move the slider while running your video
                                  * 100);

On running the above program on an offline IDE, the Media Player would look something like:

JavaFX based Media Player

We have three classes over here-
1. Main class to launch our program
2. Player class to play our Media player
3. MediaBar class to add control to our control panel of media bar.

In our class which extends over the application class we have two methods one main method to launch the program and start method where we can set the stage and media control border. Then we have added the slider at the bottom for controlling the time of video and to control volume. In order to add functionality like to jump to a particular section of video we have used the-

And for changing the media


has been used in the above program. For styling purpose we have come up with the CSS which is depicted in


Among the best features of JavaFX is that one can control formatting with Cascading Style Sheets (CSS). We have used the .getStatus() function to check the status of player that whether it is halted, playing, stopped or paused and to take action accordingly.

Note:While importing select always the JavaFX files.

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads