public void start() { movie = new MediaPlayer(path); movie.play(); VIEW.setMediaPlayer(movie); movie .currentTimeProperty() .addListener( new ChangeListener<javafx.util.Duration>() { @Override public void changed( ObservableValue<? extends javafx.util.Duration> observable, javafx.util.Duration duration, javafx.util.Duration current) { timeSlider.setValue(current.toSeconds()); } }); movie.setOnReady( new Runnable() { @Override public void run() { timeSlider.setMin(0); timeSlider.setValue(0.0); timeSlider.setMax(movie.getTotalDuration().toSeconds()); volumeSlider.setMin(0); volumeSlider.setValue(0.5); } }); timeSlider.setOnMouseClicked( new EventHandler<MouseEvent>() { @Override public void handle(MouseEvent event) { movie.seek(javafx.util.Duration.seconds(timeSlider.getValue())); } }); volumeSlider .valueProperty() .addListener( new InvalidationListener() { @Override public void invalidated(Observable observable) { if (volumeSlider.isValueChanging()) { movie.setVolume(volumeSlider.getValue() / 100); } } }); }
public MediaControl(final MediaPlayer mp) { this.mp = mp; setStyle("-fx-background-color: #bfc2c7;"); // TODO: Use css file mediaView = new MediaView(mp); mvPane = new Pane(); mvPane.getChildren().add(mediaView); mvPane.setStyle("-fx-background-color: black;"); // TODO: Use css file setCenter(mvPane); mediaBar = new HBox(5.0); mediaBar.setPadding(new Insets(5, 10, 5, 10)); mediaBar.setAlignment(Pos.CENTER_LEFT); BorderPane.setAlignment(mediaBar, Pos.CENTER); final Button playButton = ButtonBuilder.create().minWidth(Control.USE_PREF_SIZE).build(); playButton.setGraphic(imageViewPlay); playButton.setOnAction( new EventHandler<ActionEvent>() { public void handle(ActionEvent e) { updateValues(); MediaPlayer.Status status = mp.getStatus(); if (status == MediaPlayer.Status.UNKNOWN || status == MediaPlayer.Status.HALTED) { // don't do anything in these states return; } if (status == MediaPlayer.Status.PAUSED || status == MediaPlayer.Status.READY || status == MediaPlayer.Status.STOPPED) { // rewind the movie if we're sitting at the end if (atEndOfMedia) { mp.seek(mp.getStartTime()); atEndOfMedia = false; playButton.setGraphic(imageViewPlay); // playButton.setText(">"); updateValues(); } mp.play(); playButton.setGraphic(imageViewPause); // playButton.setText("||"); } else { mp.pause(); } } }); mp.currentTimeProperty() .addListener( new ChangeListener<Duration>() { @Override public void changed( ObservableValue<? extends Duration> observable, Duration oldValue, Duration newValue) { updateValues(); } }); mp.setOnPlaying( new Runnable() { public void run() { if (stopRequested) { mp.pause(); stopRequested = false; } else { playButton.setGraphic(imageViewPause); // playButton.setText("||"); } } }); mp.setOnPaused( new Runnable() { public void run() { playButton.setGraphic(imageViewPlay); // playButton.setText("||"); } }); mp.setOnReady( new Runnable() { public void run() { duration = mp.getMedia().getDuration(); updateValues(); } }); mp.setCycleCount(repeat ? MediaPlayer.INDEFINITE : 1); mp.setOnEndOfMedia( new Runnable() { public void run() { if (!repeat) { playButton.setGraphic(imageViewPlay); // playButton.setText(">"); stopRequested = true; atEndOfMedia = true; } } }); mediaBar.getChildren().add(playButton); // Time label Label timeLabel = new Label("Time"); timeLabel.setMinWidth(Control.USE_PREF_SIZE); mediaBar.getChildren().add(timeLabel); // Time slider timeSlider = SliderBuilder.create().minWidth(30).maxWidth(Double.MAX_VALUE).build(); HBox.setHgrow(timeSlider, Priority.ALWAYS); timeSlider .valueProperty() .addListener( new InvalidationListener() { public void invalidated(Observable ov) { if (timeSlider.isValueChanging()) { // multiply duration by percentage calculated by slider position if (duration != null) { mp.seek(duration.multiply(timeSlider.getValue() / 100.0)); } updateValues(); } } }); mediaBar.getChildren().add(timeSlider); // Play label playTime = LabelBuilder.create() // .prefWidth(130) .minWidth(Control.USE_PREF_SIZE) .build(); mediaBar.getChildren().add(playTime); // Fullscreen button Button buttonFullScreen = ButtonBuilder.create().text("Full Screen").minWidth(Control.USE_PREF_SIZE).build(); buttonFullScreen.setOnAction( new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent event) { if (!fullScreen) { newStage = new Stage(); newStage .fullScreenProperty() .addListener( new ChangeListener<Boolean>() { @Override public void changed( ObservableValue<? extends Boolean> ov, Boolean t, Boolean t1) { onFullScreen(); } }); final BorderPane borderPane = new BorderPane() { @Override protected void layoutChildren() { if (mediaView != null && getBottom() != null) { mediaView.setFitWidth(getWidth()); mediaView.setFitHeight(getHeight() - getBottom().prefHeight(-1)); } super.layoutChildren(); if (mediaView != null) { mediaView.setTranslateX( (((Pane) getCenter()).getWidth() - mediaView.prefWidth(-1)) / 2); mediaView.setTranslateY( (((Pane) getCenter()).getHeight() - mediaView.prefHeight(-1)) / 2); } }; }; setCenter(null); setBottom(null); borderPane.setCenter(mvPane); borderPane.setBottom(mediaBar); Scene newScene = new Scene(borderPane); newStage.setScene(newScene); // Workaround for disposing stage when exit fullscreen newStage.setX(-100000); newStage.setY(-100000); newStage.setFullScreen(true); fullScreen = true; newStage.show(); } else { // toggle FullScreen fullScreen = false; newStage.setFullScreen(false); } } }); mediaBar.getChildren().add(buttonFullScreen); // Volume label Label volumeLabel = new Label("Vol"); volumeLabel.setMinWidth(Control.USE_PREF_SIZE); mediaBar.getChildren().add(volumeLabel); // Volume slider volumeSlider = SliderBuilder.create().prefWidth(70).minWidth(30).maxWidth(Region.USE_PREF_SIZE).build(); volumeSlider .valueProperty() .addListener( new InvalidationListener() { public void invalidated(Observable ov) {} }); volumeSlider .valueProperty() .addListener( new ChangeListener<Number>() { @Override public void changed( ObservableValue<? extends Number> observable, Number oldValue, Number newValue) { if (volumeSlider.isValueChanging()) { mp.setVolume(volumeSlider.getValue() / 100.0); } } }); mediaBar.getChildren().add(volumeSlider); setBottom(mediaBar); }
public MediaControl(final MediaPlayer mp) { this.mp = mp; setStyle("-fx-background-color: #bfc2c7;"); // TODO: Use css file mediaView = new MediaView(mp); Pane mvPane = new Pane() {}; mvPane.getChildren().add(mediaView); mvPane.setStyle("-fx-background-color: black;"); // TODO: Use css file setCenter(mvPane); mediaBar = new HBox(); mediaBar.setPadding(new Insets(5, 10, 5, 10)); mediaBar.setAlignment(Pos.CENTER_LEFT); BorderPane.setAlignment(mediaBar, Pos.CENTER); final Button playButton = new Button(); playButton.setGraphic(imageViewPlay); playButton.setOnAction( new EventHandler<ActionEvent>() { public void handle(ActionEvent e) { updateValues(); Status status = mp.getStatus(); if (status == Status.UNKNOWN || status == Status.HALTED) { // don't do anything in these states return; } if (status == Status.PAUSED || status == Status.READY || status == Status.STOPPED) { // rewind the movie if we're sitting at the end if (atEndOfMedia) { mp.seek(mp.getStartTime()); atEndOfMedia = false; playButton.setGraphic(imageViewPlay); // playButton.setText(">"); updateValues(); } mp.play(); playButton.setGraphic(imageViewPause); // playButton.setText("||"); } else { mp.pause(); } } }); mp.currentTimeProperty() .addListener( new ChangeListener<Duration>() { @Override public void changed( ObservableValue<? extends Duration> observable, Duration oldValue, Duration newValue) { updateValues(); } }); mp.setOnPlaying( new Runnable() { public void run() { // System.out.println("onPlaying"); if (stopRequested) { mp.pause(); stopRequested = false; } else { playButton.setGraphic(imageViewPause); // playButton.setText("||"); } } }); mp.setOnPaused( new Runnable() { public void run() { // System.out.println("onPaused"); playButton.setGraphic(imageViewPlay); // playButton.setText("||"); } }); mp.setOnReady( new Runnable() { public void run() { duration = mp.getMedia().getDuration(); updateValues(); } }); mp.setCycleCount(repeat ? MediaPlayer.INDEFINITE : 1); mp.setOnEndOfMedia( new Runnable() { public void run() { if (!repeat) { playButton.setGraphic(imageViewPlay); // playButton.setText(">"); stopRequested = true; atEndOfMedia = true; } } }); mediaBar.getChildren().add(playButton); // Add spacer Label spacer = new Label(" "); mediaBar.getChildren().add(spacer); // Time label Label timeLabel = new Label("Time: "); timeLabel.setMinWidth(Control.USE_PREF_SIZE); mediaBar.getChildren().add(timeLabel); // Time slider timeSlider = new Slider(); HBox.setHgrow(timeSlider, Priority.ALWAYS); timeSlider.setMinWidth(50); timeSlider.setMaxWidth(Double.MAX_VALUE); timeSlider .valueProperty() .addListener( new InvalidationListener() { public void invalidated(Observable ov) { if (timeSlider.isValueChanging()) { // multiply duration by percentage calculated by slider position if (duration != null) { mp.seek(duration.multiply(timeSlider.getValue() / 100.0)); } updateValues(); } } }); mediaBar.getChildren().add(timeSlider); // Play label playTime = new Label(); playTime.setPrefWidth(130); playTime.setMinWidth(50); mediaBar.getChildren().add(playTime); // Volume label Label volumeLabel = new Label("Vol: "); volumeLabel.setMinWidth(Control.USE_PREF_SIZE); mediaBar.getChildren().add(volumeLabel); // Volume slider volumeSlider = new Slider(); volumeSlider.setPrefWidth(70); volumeSlider.setMaxWidth(Region.USE_PREF_SIZE); volumeSlider.setMinWidth(30); volumeSlider .valueProperty() .addListener( new InvalidationListener() { public void invalidated(Observable ov) {} }); volumeSlider .valueProperty() .addListener( new ChangeListener<Number>() { @Override public void changed( ObservableValue<? extends Number> observable, Number oldValue, Number newValue) { if (volumeSlider.isValueChanging()) { mp.setVolume(volumeSlider.getValue() / 100.0); } } }); mediaBar.getChildren().add(volumeSlider); setBottom(mediaBar); }
private void onPlay() { playButton.setImage(new Image("icon/PlayPause_button/pause.png")); mp.setOnReady( () -> { duration = mp.getMedia().getDuration(); updateValues(); }); mp.currentTimeProperty() .addListener( (Observable ov) -> { Stage stage = (Stage) root.getScene().getWindow(); if (stage.isFullScreen() == false) { full = false; } updateValues(); }); mp.setOnPaused( () -> { playButton.setImage(new Image("icon/PlayPause_button/play.png")); }); mp.setOnEndOfMedia( () -> { switch (repeat) { case 0: if (fileList.size() - 1 == 0) { mp.stop(); mp.seek(mp.getStartTime()); } else { listView.getSelectionModel().selectNext(); } break; case 1: mp.seek(Duration.ZERO); mp.setCycleCount(MediaPlayer.INDEFINITE); break; case 2: if (j == fileList.size() - 1) { if (fileList.size() - 1 == 0) { mp.setCycleCount(MediaPlayer.INDEFINITE); } else { listView.getSelectionModel().selectFirst(); } } else { listView.getSelectionModel().selectNext(); } break; } atEndOfMedia = true; }); timeSlider .valueProperty() .addListener( (Observable ov) -> { if (timeSlider.isValueChanging()) { progressBar.setProgress(timeSlider.getValue() / 100); mp.seek(duration.multiply(timeSlider.getValue() / 100.0)); } }); volumeSlider .valueProperty() .addListener( (ObservableValue<? extends Number> ov, Number old_val, Number new_val) -> { mp.setVolume(new_val.doubleValue()); volumeBar.setProgress(new_val.doubleValue()); }); play = true; volumeSlider.setMouseTransparent(!play); timeSlider.setMouseTransparent(!play); }
/* * Function that creates a play button, adds the relevant event handlers for it, * and adds it to the appropiate media control bar. Done for both normal view mode * and fullscreen mode. */ void setPlayButton() { // Create a play button. playButton = new Button(); playButton.setGraphic(new ImageView(playImage)); playButton.setOnAction( new EventHandler<ActionEvent>() { // ActionHandler for play button. public void handle(ActionEvent e) { updateValues(); Status status = mp.getStatus(); // Check for bad status's. if (status == Status.UNKNOWN || status == Status.HALTED) { System.out.println("Player is in a bad or unknown state, can't play."); return; } // Check for accepted status's. if (status == Status.PAUSED || status == Status.READY || status == Status.STOPPED) { // Rewind the video if it's at the end if (atEndOfMedia) { mp.seek(mp.getStartTime()); atEndOfMedia = false; playButton.setGraphic(new ImageView(playImage)); updateValues(); } // Set video to play again and set pause image for button mp.play(); playButton.setGraphic(new ImageView(pauseImage)); } else { // Pause the media and set play image for button mp.pause(); playButton.setGraphic(new ImageView(playImage)); } } }); // Play/Pause Button in fullscreen mode playButtonFS = new Button(); playButtonFS.setGraphic(new ImageView(playImage)); playButtonFS.setOnAction( new EventHandler<ActionEvent>() { // ActionHandler for play button in fullscreen mode. public void handle(ActionEvent e) { updateValues(); Status status = mp.getStatus(); // Check for bad status's if (status == Status.UNKNOWN || status == Status.HALTED) { System.out.println("Player is in a bad or unknown state, can't play."); return; } // Check for acceptable status's if (status == Status.PAUSED || status == Status.READY || status == Status.STOPPED) { // rewind the movie if we're sitting at the end if (atEndOfMedia) { mp.seek(mp.getStartTime()); atEndOfMedia = false; playButtonFS.setGraphic(new ImageView(playImage)); updateValues(); } // Set video to play again and set pause image for button mp.play(); playButtonFS.setGraphic(new ImageView(pauseImage)); } else { // Pause the media and set play image for button mp.pause(); playButtonFS.setGraphic(new ImageView(playImage)); } } }); // Whenever there's a change in duration of the MediaPlayer, update the Time Label and Slider // Position mp.currentTimeProperty() .addListener( new ChangeListener<Duration>() { @Override public void changed( ObservableValue<? extends Duration> observableValue, Duration duration, Duration current) { updateValues(); } }); // Media is playing so set appropiate playButton image mp.setOnPlaying( new Runnable() { public void run() { if (stopRequested) { // If a media stop has been requested in the meantime, pause the media. mp.pause(); stopRequested = false; } else { // Otherwise set the pause image for the "play" button playButton.setGraphic(new ImageView(pauseImage)); playButtonFS.setGraphic(new ImageView(pauseImage)); } } }); // Media is paused so set appropiate playButton image mp.setOnPaused( new Runnable() { public void run() { // Set the play image for the "play" button playButton.setGraphic(new ImageView(playImage)); playButtonFS.setGraphic(new ImageView(playImage)); } }); // Media is stopped so set appropiate playButton image mp.setOnStopped( new Runnable() { public void run() { // Media has stopped so display the play image for the "play" button mp.setStopTime(Duration.INDEFINITE); atEndOfMedia = true; playButton.setGraphic(new ImageView(playImage)); playButtonFS.setGraphic(new ImageView(playImage)); } }); // Media is ready so get the total duration of the Media and update the Time Label mp.setOnReady( new Runnable() { public void run() { duration = mp.getMedia().getDuration(); updateValues(); } }); // Media has finished playing so set appropiate playButton image & handle loops mp.setOnEndOfMedia( new Runnable() { public void run() { if (!mpLoop) { // If loop not set then stop media and display play image for "play" button playButton.setGraphic(new ImageView(playImage)); playButtonFS.setGraphic(new ImageView(playImage)); atEndOfMedia = true; mp.stop(); } // Otherwise go back to the requested start time of the media mp.seek(mp.getStartTime()); } }); // Media is repeating so set appropiate playButton image mp.setOnRepeat( new Runnable() { public void run() { // Display the pause image for the "play" button atEndOfMedia = false; playButton.setGraphic(new ImageView(pauseImage)); playButtonFS.setGraphic(new ImageView(pauseImage)); } }); // Add the play button to the MediaControl bar. mediaBar.getChildren().add(playButton); }