Reputation: 79
I have a main class that sets the scene and stage
public class MediaPlayer extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("Main-UI.fxml"));
stage.setTitle("Media Player");
stage.setScene(new Scene(root));
stage.show();
}
}
I also have a controller class where i implement all most my function and ect . For some reason my setOnMouseClicked do not seem to work. Is this because it is not the same file that calls the stage.show()
Here is an example of the function the mouse event does not work.
private void setMediaViews(){
String path = "E:" + File.separator + "Videos" + File.separator + "UFC.205.PPV.Alvarez.vs.McGregor.HDTV.x264-Ebi.mp4";
media = new Media(new File(path).toURI().toString());
player = new MediaPlayer(media);
view = new MediaView(player);
VBox vbox = new VBox();
Slider slider = new Slider();
vbox.getChildren().add(slider);
videoMediaCont.setCenter(view);
videoMediaCont.setBottom(vbox);
player.play();
player.setOnReady(new Runnable() {
@Override
public void run() {
int w = player.getMedia().getWidth();
int h = player.getMedia().getHeight();
view.setFitWidth(900);
vbox.setAlignment(Pos.CENTER);
vbox.setMinHeight(50);
slider.setMaxSize(900,50);
slider.setMin(0.0);
slider.setValue(0.0);
slider.setMax(player.getTotalDuration().toSeconds());
slider.setOnMouseClicked(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
player.seek(Duration.seconds(slider.getValue()));
}
});
}
});
player.currentTimeProperty().addListener(new ChangeListener<Duration>() {
@Override
public void changed(ObservableValue<? extends Duration> observable, Duration oldValue, Duration current) {
slider.setValue(current.toSeconds());
}
});
}
Any help would be great thank you.
Upvotes: 0
Views: 9702
Reputation: 1776
I see that you try to change the value of the slider, as mentionned in docs replaces your "OnMouseClicked" event by :
slider.valueProperty().addListener(new ChangeListener<Number>() {
public void changed(ObservableValue<? extends Number> ov,
Number old_val, Number new_val) {
player.seek(Duration.seconds((double) new_val);
}});
Upvotes: 2