简体   繁体   中英

JavaFX MediaPlayer just stops after 1 minute+

I'm using the MediaPlayer class in JavaFX to run the media the whole time untill you close the application but the MediaPlayer just stops after 1 minute and the file is 11 minutes long. This is my code:

@Override
    public void start(Stage primaryStage) throws Exception {
        FileChooser chooser = new FileChooser();
        File file = chooser.showOpenDialog(primaryStage);
        Media media = null;
        if(file != null) {
            media = new Media(file.toURI().toString());
        }
        MediaPlayer mediaPlayer = new MediaPlayer(media);
        mediaPlayer.setAutoPlay(true);
        mediaPlayer.play();
        Group root = new Group();
        Scene scene = new Scene(root, 600, 400);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

Hotfix , try adding the following code inside start()

primaryStage.setOnCloseRequest(windowEvent -> {
    mediaPlayer.stop();
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM