在javafx中,如果我关闭并重新打开场景,我的媒体播放器会冻结

时间:2016-11-08 19:35:11

标签: javafx javafx-8

我和我的朋友正在编写一个小型的javafx 8程序,用于拉扯人们。一旦你打开它,如果你试图关闭它再次回来,拖动它也不起作用。我们遇到了一个问题:一旦你启动程序然后将其关闭,1秒后它会重新打开,但媒体播放器会冻结,我们无法找出原因。编辑:重新打开时,媒体播放器不会立即冻结,重新打开后1秒钟冻结。所以它重新打开,播放得很好,但是如果你等了,让它在重新打开后播放1秒钟,它会冻结。

这就是媒体播放器的创建方式:

    Media media = new Media(getClass().getResource("/res/video.mp4").toString());
    mediaPlayer = new javafx.scene.media.MediaPlayer(media);
    mediaPlayer.setAutoPlay(true);
    mediaPlayer.setCycleCount(MediaPlayer.INDEFINITE);
    MediaView mediaView = new MediaView(mediaPlayer);

当窗口关闭时,我们使用以下代码暂停视频1秒钟:

  primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>() {
        @Override
        public void handle(WindowEvent we) {
        primaryStage.close();
        mediaPlayer.pause();
            System.out.println("pause");
        timeline = new Timeline(new KeyFrame(
            Duration.millis(1000),
            e -> {
                openWindow(primaryStage);
            }
        ));
        timeline.setCycleCount(1);
        timeline.play();
        }
    }); 

最后,openWindow方法在1秒后打开所有内容:

    private void openWindow(Stage primaryStage){
    primaryStage.show();
    mediaPlayer.play();
}

我遗漏了一些东西,因为我觉得这是不合情理的。如果你认为你需要更多,我愿意把我的完整代码放在这里,但这是101行,这似乎有点超过顶部。 我想提前感谢你的帮助,如果问题不清楚,我很抱歉:我真的没有经验...... 亲切的问候 路易梅宁

编辑:这是我的完整代码:

    package rickroll;

    import java.util.ArrayList;
    import javafx.animation.KeyFrame;
    import javafx.animation.Timeline;
    import javafx.application.Application;
import javafx.application.Platform;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.input.MouseEvent;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaPlayer.Status;
import static javafx.scene.media.MediaPlayer.Status.PLAYING;
import javafx.scene.media.MediaView;
import javafx.stage.Screen;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;
import javafx.util.Duration;

public class Rickroll extends Application {
    private ArrayList<Stage> stages = new ArrayList<>();
    private Group root = new Group();
    private MediaPlayer mediaPlayer;
    private Timeline focusTimer, timeline;
    private Scene scene;

    @Override
    public void start(Stage primaryStage) {
        Media media = new Media(getClass().getResource("/res/video.mp4").toString());
        mediaPlayer = new javafx.scene.media.MediaPlayer(media);
//        mediaPlayer.setAutoPlay(false);
        mediaPlayer.setCycleCount(MediaPlayer.INDEFINITE);
        MediaView mediaView = new MediaView(mediaPlayer);
        stages.add(primaryStage);
        primaryStage.setAlwaysOnTop(true);
        Button button = new Button();
        button.addEventHandler(MouseEvent.MOUSE_CLICKED, e -> Platform.exit());

        for(int i = 1; Screen.getScreens().size() >= i; i++){

        }

        root.getChildren().add(mediaView);  
        root.getChildren().add(button);
        scene = new Scene(
            root, 
            Screen.getPrimary().getBounds().getWidth(), 
            Screen.getPrimary().getBounds().getWidth()
        );
        primaryStage.setScene(scene);
        Platform.setImplicitExit(false);

        focusTimer = new Timeline(new KeyFrame(
            Duration.millis(500),
            e -> {
                primaryStage.requestFocus();
                primaryStage.centerOnScreen();
                primaryStage.setMaximized(true);
            }
        ));
        focusTimer.setCycleCount(Timeline.INDEFINITE);
        focusTimer.play();

        primaryStage.setTitle("You just got Rick Rolled!");
        openWindow(primaryStage);

        primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>() {
            @Override
            public void handle(WindowEvent we) {
            primaryStage.hide();
            mediaPlayer.pause();
                System.out.println("pause");
            timeline = new Timeline(new KeyFrame(
                Duration.millis(1000),
                e -> {
                    openWindow(primaryStage);
                }
            ));
            timeline.setCycleCount(1);
            timeline.play();
            focusTimer.pause();
            }
        }); 

    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

    private void openWindow(Stage primaryStage){
        primaryStage.show();
        mediaPlayer.play();
        System.out.println("play");
        focusTimer.play();
    }

}

正如您将看到的,那里有一些问题不在于此。比如在多个屏幕上执行此操作的开始,以及关闭窗口的临时按钮。请忽略那部分。

2 个答案:

答案 0 :(得分:0)

如果问题是它在打开/关闭之间暂停,那么因为你的mediaPlayer.pause()处理程序中有onCloseRequest。但是,如果问题是它再次打开时没有重新启动我无法复制它。还有什么是for循环的目的?

答案 1 :(得分:0)

检查下面的代码(它包含注释):

import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

import javafx.animation.KeyFrame;
import javafx.animation.PauseTransition;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.input.MouseEvent;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import javafx.stage.Screen;
import javafx.stage.Stage;
import javafx.util.Duration;

public class Rickroll extends Application {

    private ArrayList<Stage>    stages  = new ArrayList<>();

    private Group               root    = new Group();
    private Scene               scene;

    private MediaPlayer         mediaPlayer;
    private Timeline            focusTimer;

    @Override
    public void start(Stage primaryStage) {

        // Initialise the MediaPlayer
        Media media = null;
        try {
            //change here with your video path
            media = new Media(getClass().getResource("/videos/video.mp4").toURI().toString());
            mediaPlayer = new MediaPlayer(media);
            mediaPlayer.setCycleCount(MediaPlayer.INDEFINITE);
            // mediaPlayer.setAutoPlay(false)
        } catch (URISyntaxException ex) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex);
        }

        // Create the Media View
        MediaView mediaView = new MediaView(mediaPlayer);

        // Assuming that you want the Media to have the same size as the window
        //primaryStage.widthProperty()
        //      .addListener((observable, oldValue, newValue) -> mediaView.setFitWidth(primaryStage.getWidth()))

        //primaryStage.heightProperty()
        //      .addListener((observable, oldValue, newValue) -> mediaView.setFitHeight(primaryStage.getHeight()))

        // ?
        stages.add(primaryStage);

        // PrimaryStage
        primaryStage.setTitle("You just got Rick Rolled!");
        primaryStage.setOnCloseRequest(windowEvent -> {

            // Hide the stage
            primaryStage.hide();

            // Pause the Media Player
            mediaPlayer.pause();
            System.out.println("pause");

            // Use a pause transition to wait 1 second - 1000 milliseconds
            PauseTransition pause = new PauseTransition();
            pause.setDuration(Duration.millis(1000));
            pause.play();
            pause.setOnFinished(finish -> openWindow(primaryStage));

            // Pause the focusTimer
            focusTimer.pause();
        });

        // Button
        Button button = new Button();
        button.addEventHandler(MouseEvent.MOUSE_CLICKED, e -> Platform.exit());

        // ? you want to know the primary screen size?
        for (int i = 1; Screen.getScreens().size() >= i; i++) {

        }

        // Add the children to the root
        root.getChildren().addAll(mediaView, button);
        // Scene
        scene = new Scene(root, Screen.getPrimary().getBounds().getWidth(), Screen.getPrimary().getBounds().getWidth());
        primaryStage.setScene(scene);
        Platform.setImplicitExit(false);

        // Focus Timer
        focusTimer = new Timeline(new KeyFrame(Duration.millis(500), e -> {
            primaryStage.requestFocus();
            primaryStage.centerOnScreen();
            primaryStage.setMaximized(true);
        }));
        focusTimer.setCycleCount(Timeline.INDEFINITE);
        focusTimer.play();

        // Open the primary stage
        openWindow(primaryStage);

    }

    /**
     * @param args
     *            the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

    /**
     * Open the window and start the player
     * 
     * @param primaryStage
     */
    private void openWindow(Stage primaryStage) {

        // Show the primary stage
        primaryStage.show();

        // start the media player
        mediaPlayer.play();
        System.out.println("play");

        // start the focusTimer
        focusTimer.play();
    }

}