play video when mouse is idle. and stop video when mouse is clicked/move JAVAFX

123 Views Asked by At

I am trying to implement a feature where the program plays a video in fullscreen when there is no mouseclick or mousemove, say for x seconds. and stops the video and go back to previous scene when mouse is clicked or moved

currently i have this one working.. BUT the video plays after 5 seconds even though i click and move the mouse.. and I can't seem to find a solution on how to close the video and proceed to the previous scene/fxml when mouse is clicked move..

current code as of writing:

for playing video when mouse is idle:

PauseTransition delay = new PauseTransition(Duration.seconds(5));
delay.setOnFinished( event -> {
            try {
                Main.showVideo();
            } catch (IOException ex) {
                Logger.getLogger(UserMainPage2Controller.class.getName()).log(Level.SEVERE, null, ex);
            }
        } );
delay.play();

for showing video (located inside my main class):

public static void showVideo() throws IOException
        {


    File f = new File("C:\\vid\\saitama.mp4");

    Media media = new Media(f.toURI().toString());
    MediaView mv = new MediaView();

    MediaPlayer mp = new MediaPlayer(media);
    mv.setMediaPlayer(mp);

    FXMLLoader loader=new FXMLLoader();
    loader.setLocation(Main.class.getResource("page/videoPlayer.fxml"));
    mainLayout = loader.load();
    StackPane root=new StackPane();
    root.getChildren().add(mv);
    stage.setScene(new Scene(root,1000,1000));
    stage.setTitle("Video");
    stage.setFullScreen(true);

    stage.show();
    mp.play();


        }   

and im not really sure what to put inside my VideoPlayercontroller class either: right now it is empty.

public class VideoPlayerController implements Initializable {


    @Override
    public void initialize(URL url, ResourceBundle rb) {



    }    

}

So what im trying to do is only play the video when mouse is idle (not clicked or moved for x seconds).. and closes the video when the mouse is moved or click.. like for example..

if mouseclicked then Main.showPreviousScene();
1

There are 1 best solutions below

0
Elijah McLain On

May be an issue that your delay is happening every time 5 seconds after first movement but not being updated before its elapsed. Try using timers instead so that you set the timer to 5 seconds on mouse moved event on timer elapsed event set the video to play.