在Catalina上播放MediaFile时,JavaFx应用程序挂起

时间:2019-12-20 15:22:01

标签: java javafx javafx-8 macos-catalina

在High Sierra和Mojave机器人上完美运行的JavaFx应用程序在Catalina上播放媒体文件时挂起。

我在这里显示java文件。我不知道该如何附加文件,否则我可以提供可以轻松复制的媒体文件。

我创建了一个Java Fx应用程序,该应用程序只有3个类用于在catalina上播放媒体文件,并且该应用程序也复制了相同的行为。 这是在OSX catalina上使用这些类的项目类文件。

我不明白为什么它仅在OSX catalina上失败。请提出此代码有什么问题。

    public class PlayingMedia extends Application {
        Player player; 
        FileChooser fileChooser; 
        public void start(final Stage primaryStage) 
        { 
            // setting up the stages 
            MenuItem open = new MenuItem("Open"); 
            Menu file = new Menu("File"); 
            MenuBar menu = new MenuBar(); 

            // Connecting the above three 
            file.getItems().add(open); // it would connect open with file 
            menu.getMenus().add(file); 

            // Adding functionality to switch to different videos 
            fileChooser = new FileChooser(); 
            open.setOnAction(new EventHandler<ActionEvent>(){ 
                public void handle(ActionEvent e) 
                { 
                    // Pausing the video while switching 
                    if(player!=null && player.player!=null)
                        player.player.pause(); 
                    File file = fileChooser.showOpenDialog(primaryStage); 

                    // Choosing the file to play 
                    if (file != null) { 
                        try { 
                            if(player!=null){
                                player.dispose();
                            }
                            player = new Player(file.toURI().toURL().toExternalForm()); 
                            player.setTop(menu);
                            Scene scene = new Scene(player, 720, 535, Color.BLACK); 
                            primaryStage.setScene(scene); 
                        } 
                        catch (MalformedURLException e1) { 
                            e1.printStackTrace(); 
                        } 
                    } 
                } 
            });
                // here you can choose any video 
                player = new Player(); 

                // Setting the menu at the top 
                player.setTop(menu);  

                // Adding player to the Scene 
                Scene scene = new Scene(player, 720, 535, Color.BLACK);  

                // height and width of the video player 
                // background color set to Black 
                primaryStage.setScene(scene); // Setting the scene to stage 
                primaryStage.show(); // Showing the stage 
        } 

         // Main function to launch the application 
         public static void main(String[] args){ 
               launch(args); 
         } 


    }

    public class MediaBar extends HBox{
    Slider time = new Slider(); // Slider for time 
        Slider vol = new Slider(); // Slider for volume 
        Button PlayButton = new Button("||"); // For pausing the player 
        Label volume = new Label("Volume: "); 
        MediaPlayer player; 

        public MediaBar(MediaPlayer play) 
        { // Default constructor taking 
            // the MediaPlayer object 
            player = play; 

            setAlignment(Pos.CENTER); // setting the HBox to center 
            setPadding(new Insets(5, 10, 5, 10)); 
            // Settih the preference for volume bar 
            vol.setPrefWidth(70); 
            vol.setMinWidth(30); 
            vol.setValue(100); 
            HBox.setHgrow(time, Priority.ALWAYS); 
            PlayButton.setPrefWidth(30); 

            // Adding the components to the bottom 

            getChildren().add(PlayButton); // Playbutton 
            getChildren().add(time); // time slider 
            getChildren().add(volume); // volume slider 
            getChildren().add(vol); 

            // Adding Functionality 
            // to play the media player 
            PlayButton.setOnAction(new EventHandler<ActionEvent>() { 
                public void handle(ActionEvent e) 
                { 
                    Status status = player.getStatus(); // To get the status of Player 
                    if (status == status.PLAYING) { 

                        // If the status is Video playing 
                        if (player.getCurrentTime().greaterThanOrEqualTo(player.getTotalDuration())) { 

                            // If the player is at the end of video 
                            player.seek(player.getStartTime()); // Restart the video 
                            player.play(); 
                        } 
                        else { 
                            // Pausing the player 
                            player.pause(); 

                            PlayButton.setText(">"); 
                        } 
                    } // If the video is stopped, halted or paused 
                    if (status == Status.HALTED || status == Status.STOPPED || status == Status.PAUSED) { 
                        player.play(); // Start the video 
                        PlayButton.setText("||"); 
                    } 
                } 
            }); 

            // Providing functionality to time slider 
            player.currentTimeProperty().addListener(new ChangeListener<Duration>() {
                @Override
                public void changed(ObservableValue<? extends Duration> observable, Duration oldValue, Duration newValue) {
                    updatesValues(); 
                }
            });

            // Inorder to jump to the certain part of video 

            time.valueProperty().addListener(new ChangeListener<Number>() {
                @Override
                public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
                    if (time.isPressed()) { // It would set the time 
                        // as specified by user by pressing 
                        player.seek(player.getMedia().getDuration().multiply(time.getValue() / 100)); 
                    } 
                }
            });

            // providing functionality to volume slider 
           vol.valueProperty().addListener(new ChangeListener<Number>() {
                @Override
                public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
                    if (vol.isPressed()) { 
                        player.setVolume(vol.getValue() / 100); // It would set the volume 
                        // as specified by user by pressing 
                    } 
                }
            });



        } 

        // Outside the constructor 
        protected void updatesValues() 
        { 
            Platform.runLater(new Runnable() { 
                public void run() 
                { 
                    // Updating to the new time value 
                    // This will move the slider while running your video 
                    time.setValue(player.getCurrentTime().toMillis()/player.getTotalDuration().toMillis() * 100); 
                } 
            }); 
        }

}

公共类Player扩展BorderPane {

public Media media;
public MediaPlayer player;
public MediaView view;
public Pane mpane;
public MediaBar bar;

public Player(){

}


public Player(String file) { // Default constructor 
    media = new Media(file);
    player = new MediaPlayer(media);
    view = new MediaView(player);
    mpane = new Pane();
    mpane.getChildren().add(view); // Calling the function getChildren 

    // inorder to add the view 
    setCenter(mpane);
    bar = new MediaBar(player); // Passing the player to MediaBar 
    setBottom(bar); // Setting the MediaBar at bottom 
    setStyle("-fx-background-color:#bfc2c7"); // Adding color to the mediabar 
    player.play(); // Making the video play 
}

public void dispose(){
    if(player!=null){
        player.stop();
        player.dispose();
    }
}

}

0 个答案:

没有答案