JavaFX | Building a Media Player - GeeksforGeeks

23 Jun, 2020
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.Implementation:
// Java program to Build a Media // Player in JavaFXimport java.io.File;import java.net.MalformedURLException;  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 applicationpublic 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        menu.getMenus().add(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                player.player.pause();                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);                        primaryStage.setScene(scene);                    }                    catch (MalformedURLException e1) {                        e1.printStackTrace();                    }                }            }              // here you can choose any video            player = new Player("file:/// F:/songs/srk.mp4");              // Setting the menu at the top            player.setTop(menu);               // 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            primaryStage.show(); // Showing the stage    }       // Main function to launch the application     public static void main(String[] args){           launch(args);     }}      import javafx.scene.layout.BorderPane;    import javafx.scene.layout.Pane;    import javafx.scene.media.Media;    import javafx.scene.media.MediaPlayer;    import javafx.scene.media.MediaView;      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            setCenter(mpane);            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            player.play(); // 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;    import javafx.scene.media.MediaPlayer;    import javafx.scene.media.MediaPlayer.Status;      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            vol.setPrefWidth(70);            vol.setMinWidth(30);            vol.setValue(100);            HBox.setHgrow(time, Priority.ALWAYS);            PlayButton.setPrefWidth(30);              // Adding the components to the bottom              getChildren().add(PlayButton); // Playbutton            getChildren().add(time); // time slider            getChildren().add(volume); // volume slider            getChildren().add(vol);              // 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                            player.seek(player.getStartTime()); // Restart the video                            player.play();                        }                        else {                            // Pausing the player                            player.pause();                              PlayButton.setText(">");                        }                    } // If the video is stopped, halted or paused                    if (status == Status.HALTED || status == Status.STOPPED || status == Status.PAUSED) {                        player.play(); // Start the video                        PlayButton.setText("||");                    }                }            });              // Providing functionality to time slider            player.currentTimeProperty().addListener(new InvalidationListener() {                public void invalidated(Observable ov)                {                    updatesValues();                }            });              // 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                        player.seek(player.getMedia().getDuration().multiply(time.getValue() / 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                    time.setValue(player.getCurrentTime().toMillis()                                      player.getTotalDuration()                                          .toMillis()                                  * 100);                }            });        }    }
On running the above program on an offline IDE, the Media Player would look something like:
ExplanationWe have three classes over here-1. Main class to launch our program2. Player class to play our Media player3. MediaBar class to add control to our control panel of media bar.
In our Main.java 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-
player.seek(player.getMedia().
                  getDuration().multiply(time.getValue()/100));
And for changing the media
 fileChooser.showOpenDialog(primaryStage) 
has been used in the above program. For styling purpose we have come up with the CSS which is depicted in
-fx-background-color:#bfc2c7
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.
References:Referred video
Akanksha_Rai
JavaFX
Java
Misc
Misc
Misc
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Object Oriented Programming (OOPs) Concept in Java
Stream In Java
HashMap in Java with Examples
Interfaces in Java
How to iterate any Map in Java
Top 10 algorithms in Interview Questions
vector::push_back() and vector::pop_back() in C++ STL
Overview of Data Structures | Set 1 (Linear Data Structures)
How to write Regular Expressions?