/* * Function that creates a volume slider, adds the relevant event handlers to it, and * adds it to the MediaControl bar. */ void setVolumeSlider() { // Create a new volume slider volumeSlider = new Slider(); volumeSlider.setManaged(true); volumeSlider.setMaxWidth(mpWidth / 6); volumeSlider.setMinWidth(20); // Detect the change in volume slider bar and sets the MediaPlayer's volume accordingly volumeSlider .valueProperty() .addListener( new ChangeListener<Number>() { @Override public void changed( ObservableValue<? extends Number> observable, Number oldValue, Number newValue) { if (volumeSlider.isValueChanging()) { // If the slider is being dragged set volume to that requested mp.setVolume(volumeSlider.getValue() / 100.0); } // Update the text labels to display updateValues(); } }); // Add the volume slider to MediaControl bar mediaBar.getChildren().add(volumeSlider); }
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); }
/* * Creates a time slider, adds the relevant event handlers for it, and adds it to * the appropiate MediaControl bar. Handles both normal viewing mode, and fullscreen * mode. */ void setTimeSlider() { // Create a new time slide timeSlider = new Slider(); timeSlider.setMaxWidth((2 * mpWidth) / 3); HBox.setHgrow(timeSlider, Priority.ALWAYS); // Allow the user to drag and position the slider timeSlider .valueProperty() .addListener( new InvalidationListener() { @Override public void invalidated(Observable ov) { // If the value is changing perform an action if (timeSlider.isValueChanging()) { // If the duration of video is not null, move to requested time if (duration != null) { mp.seek(duration.multiply(timeSlider.getValue() / 100.0)); } } } }); // Allow the user to click on the slider to jump to the desired timing timeSlider.setOnMousePressed( new EventHandler<MouseEvent>() { @Override public void handle(MouseEvent mouseEvent) { mp.seek(duration.multiply(timeSlider.getValue() / 100.0)); } }); // Create a new time slider for fullscreen mode timeSliderFS = new Slider(); timeSliderFS.setMinWidth(bounds.getWidth() - 100); timeSliderFS .valueProperty() .addListener( new InvalidationListener() { @Override public void invalidated(Observable ov) { // If the value is changing perform an action if (timeSliderFS.isValueChanging()) { // If the duration of video is not null, move to requested time if (duration != null) { mp.seek(duration.multiply(timeSliderFS.getValue() / 100.0)); } } } }); // If mouse enters time slider in fullscreen mode handle it timeSliderFS.setOnMousePressed( new EventHandler<MouseEvent>() { @Override public void handle(MouseEvent mouseEvent) { // Stop the fade transition and move to requested time fadeTransition.stop(); mp.seek(duration.multiply(timeSliderFS.getValue() / 100.0)); } }); // Add the time slider to MediaControl bar mediaBar.getChildren().add(timeSlider); }