The start
To start we have to once again modify the start , therefore replace the start with the following lines of code. This modification will allow you to add all the functionality to the buttons and convert the Media player to the NetBeans plugin in the future.
MediaPlayer mediaPlayer; private Label time; Duration duration; Button fullScreenButton; Scene scene; Media media; double width; double height; MediaView mediaView; @Override public void start(Stage primaryStage) { scene = setScene(this.width, this.height); primaryStage.setTitle("Media Player!"); primaryStage.setScene(scene); primaryStage.show(); }
Press Alt+Shift+F on your keyboard to format the code in NetBeans.
Add setScene() method
public Scene setScene(double width, double height) { this.height = height; this.width = width; //Add your own path of the vidio that you want to play String path = "F:/MediaPl/src/mediapl/Tom and Jerry Beach YouTube.mp4"; media = new Media(new File(path).toURI().toString()); mediaPlayer = new MediaPlayer(media); //AutoPlay set to false mediaPlayer.setAutoPlay(false); mediaView = new MediaView(mediaPlayer); // DropShadow effect DropShadow dropshadow = new DropShadow(); dropshadow.setOffsetY(5.0); dropshadow.setOffsetX(5.0); dropshadow.setColor(Color.WHITE); mediaView.setEffect(dropshadow); BorderPane borderPane = new BorderPane(); borderPane.setCenter(mediaView); borderPane.setBottom(addToolBar()); borderPane.setStyle("-fx-background-color: Black"); scene = new Scene(borderPane, 600, 600); scene.setFill(Color.BLACK); return scene; }
Add functionality to the Buttons:
playButton.setOnAction((ActionEvent e) -> { mediaPlayer.play(); });
pauseButton.setOnAction((ActionEvent e) -> { mediaPlayer.pause(); });
forwardButton.setOnAction((ActionEvent e) -> { mediaPlayer.seek(mediaPlayer.getCurrentTime().multiply(1.5)); });
backButton.setOnAction((ActionEvent e) -> { mediaPlayer.seek(mediaPlayer.getCurrentTime().divide(1.5)); });
firstButton.setOnAction((ActionEvent e) -> { mediaPlayer.seek(mediaPlayer.getStartTime()); mediaPlayer.stop(); });
lastButton.setOnAction((ActionEvent e) -> { mediaPlayer.seek(mediaPlayer.getTotalDuration()); mediaPlayer.stop(); });
reloadButton.setOnAction((ActionEvent e) -> { mediaPlayer.seek(mediaPlayer.getStartTime()); });
The action to this particular button has to be added to the start just before primaryStage.show(); line of code
fullScreenButton.setOnAction((ActionEvent e) -> { if (primaryStage.isFullScreen()) { primaryStage.setFullScreen(false); } else { primaryStage.setFullScreen(true); } });
filesButton.setOnAction((ActionEvent e) -> { FileChooser fc = new FileChooser(); fc.getExtensionFilters().add(new ExtensionFilter("*.flv", "*.mp4", "*.mpeg")); File file = fc.showOpenDialog(null); String path = file.getAbsolutePath(); path = path.replace("\\", "/"); media = new Media(new File(path).toURI().toString()); mediaPlayer.stop(); mediaPlayer = new MediaPlayer(media); mediaPlayer.setAutoPlay(true); mediaView.setMediaPlayer(mediaPlayer); });
time Label:
time = new Label(); time.setTextFill(Color.YELLOW); time.setPrefWidth(80); mediaPlayer.currentTimeProperty().addListener((Observable ov) -> { updateValues(); }); mediaPlayer.setOnReady(() -> { duration = mediaPlayer.getMedia().getDuration(); updateValues(); });
Add updateValues() method
protected void updateValues() { if (time != null) { runLater(() -> { Duration currentTime = mediaPlayer.getCurrentTime(); time.setText(formatTime(currentTime, duration)); }); } }
Add formatTime() method
private static String formatTime(Duration elapsed, Duration duration) { int intElapsed = (int) floor(elapsed.toSeconds()); int elapsedHours = intElapsed / (60 * 60); if (elapsedHours > 0) { intElapsed -= elapsedHours * 60 * 60; } int elapsedMinutes = intElapsed / 60; int elapsedSeconds = intElapsed - elapsedHours * 60 * 60 - elapsedMinutes * 60; if (duration.greaterThan(Duration.ZERO)) { int intDuration = (int) floor(duration.toSeconds()); int durationHours = intDuration / (60 * 60); if (durationHours > 0) { intDuration -= durationHours * 60 * 60; } int durationMinutes = intDuration / 60; int durationSeconds = intDuration - durationHours * 60 * 60 - durationMinutes * 60; if (durationHours > 0) { return format("%d:%02d:%02d/%d:%02d:%02d", elapsedHours, elapsedMinutes, elapsedSeconds, durationHours, durationMinutes, durationSeconds); } else { return format("%02d:%02d/%02d:%02d", elapsedMinutes, elapsedSeconds, durationMinutes, durationSeconds); } } else { if (elapsedHours > 0) { return format("%d:%02d:%02d", elapsedHours, elapsedMinutes, elapsedSeconds); } else { return format("%02d:%02d", elapsedMinutes, elapsedSeconds); } } }
The updateValues and formatTime methods was taken from the Oracle tutorial: “3 Controlling Media Playback” which can be found here.
All done!
So that is all the code that allows you to create Media Player in JavaFx. You can also create a NetBeans plugin using this code. If your interested on how to do so please follow my previous articles:
How to write a WebBrowser plugin in JavaFX for the NetBeans IDE
I hope you found this guide useful, let us know what you think.