How do you rewrite the MediaView after the new psychics?



  • The program provides for the possibility of opening other media files, but after choosing the next file and starting it, there's only a sound, and the video card is not updated, how does it go to JavaFX?

    public class MediaPlayerMain extends Application {
    

    static Stage stage;

    @Override
    public void start(Stage stage) {
    try {
    MediaPlayerMain.stage = stage;
    URL url = getClass().getResource("/application/MediaPlayerView.fxml");
    BorderPane pane = FXMLLoader.load(url);
    Scene scene = new Scene(pane);
    stage.setTitle("Media Player");
    stage.setScene(scene);
    stage.show();
    } catch (Exception e) {
    e.printStackTrace();
    }
    }

    public static Stage getStage(){
    return stage;
    }

    public static void main(String[] args) {
    launch(args);
    }
    }

    public class MediaPlayerController implements Initializable {
    @FXML
    ToolBar toolBar;

    @FXML
    CheckBox checkBox;

    @FXML
    Slider slider;

    @FXML
    Button btnPlay, btnPause, btnStop, btnOpen;

    @FXML
    BorderPane borderPane;

    private Media media;
    private MediaPlayer mediaPlayer;
    private MediaView mediaView;

    public void initialize(URL location, ResourceBundle resources) {
    borderPane.setCenter(createMediaView());
    toolBar = new ToolBar(btnPlay, btnPause, btnStop, btnOpen, slider, checkBox);
    borderPane.setBottom(toolBar);
    MyEvent event = new MyEvent();
    btnPlay.setOnAction(event);
    btnPause.setOnAction(event);
    btnStop.setOnAction(event);
    btnOpen.setOnAction(event);
    }

    public String getPath(){
    FileChooser fc = new FileChooser();
    fc.getExtensionFilters().add(new ExtensionFilter("Media files",".flv", ".mp4", "*.mpeg"));
    File file = fc.showOpenDialog(null);
    String path = file.getAbsolutePath();
    path = path.replace("\", "/");
    return path;
    }

    public MediaView createMediaView(){
    media = new Media(new File(getPath()).toURI().toString());
    mediaPlayer = new MediaPlayer(media);
    mediaView = new MediaView(mediaPlayer);
    mediaPlayer.setAutoPlay(true);
    return mediaView;
    }

    public void fullScreen(ActionEvent event){
    if(checkBox.isSelected()){
    MediaPlayerMain.getStage().setFullScreen(true);
    System.out.println("Fullscreen true");
    }else{
    MediaPlayerMain.getStage().setFullScreen(false);
    System.out.println("Fullscreen false");
    }
    }

    class MyEvent implements EventHandler<ActionEvent> {

    public void handle(ActionEvent event) {
        Button btn = (Button) event.getSource();
        if (btn.equals(btnPlay)) {
            mediaPlayer.play();
        } else if (btn.equals(btnPause)) {
            mediaPlayer.pause();
        } else if (btn.equals(btnStop)) {
            mediaPlayer.stop();
        } else if (btn.equals(btnOpen)){
            if(mediaPlayer.getStatus().equals(Status.PLAYING)){
                mediaPlayer.pause();
                createMediaView().setMediaPlayer(new MediaPlayer(media));
            }
        }
        System.out.println(btn.getText());
    }
    

    }
    }



  • Well, no one's told me, found his own decision. The code changed a little, added a method somewhere, merged somewhere. MediaPlayerMain did not change, changes were added only to MediaPlayerController.

    public class MediaPlayerController implements Initializable {
    @FXML
    ToolBar toolBar;
    

    @FXML
    CheckBox checkBox;

    @FXML
    Slider slider;

    @FXML
    Button btnPlay, btnPause, btnStop, btnOpen;

    @FXML
    BorderPane borderPane;

    private Media media;
    private MediaPlayer mediaPlayer;
    private MediaView mediaView;

    public void initialize(URL location, ResourceBundle resources) {
    borderPane.setCenter(createMediaView());
    createToolbar();
    MyEvent event = new MyEvent();
    btnPlay.setOnAction(event);
    btnPause.setOnAction(event);
    btnStop.setOnAction(event);
    btnOpen.setOnAction(event);
    }

    public void createToolbar() {
    toolBar = new ToolBar(btnPlay, btnPause, btnStop, btnOpen, slider, checkBox);
    borderPane.setBottom(toolBar);
    }

    public MediaView createMediaView() {
    FileChooser fc = new FileChooser();
    fc.getExtensionFilters().add(new ExtensionFilter("Media files", ".flv", ".mp4", "*.mpeg"));
    File file = fc.showOpenDialog(null);
    String path = file.getAbsolutePath();
    path = path.replace("\", "/");
    media = new Media(new File(path).toURI().toString());
    mediaPlayer = new MediaPlayer(media);
    mediaView = new MediaView(mediaPlayer);
    mediaPlayer.setAutoPlay(true);
    return mediaView;
    }

    public void fullScreen(ActionEvent event) {
    if (checkBox.isSelected()) {
    MediaPlayerMain.getStage().setFullScreen(true);
    System.out.println("Fullscreen true");
    } else {
    MediaPlayerMain.getStage().setFullScreen(false);
    System.out.println("Fullscreen false");
    }
    }

    class MyEvent implements EventHandler<ActionEvent> {

    public void handle(ActionEvent event) {
        Button btn = (Button) event.getSource();
        if (btn.equals(btnPlay)) {
            mediaPlayer.play();
        } else if (btn.equals(btnPause)) {
            mediaPlayer.pause();
        } else if (btn.equals(btnStop)) {
            mediaPlayer.stop();
        } else if (btn.equals(btnOpen)) {
            if (mediaPlayer.getStatus().equals(Status.PLAYING)) {
                mediaPlayer.pause();
                borderPane.setCenter(createMediaView());
                createToolbar();
            }
        }
        System.out.println(btn.getText());
    }
    

    }
    }




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2