如何在Javafx的堆栈窗格中播放媒体?

时间:2017-02-07 23:34:12

标签: java javafx javafx-8

我正在学习如何使用JavaFx,我正在尝试创建一个有两个场景的媒体播放器,其中一个播放视频而另一个播放音乐。我正在处理视频场景,无法弄清楚为什么我的代码无效。

public class MediaApp extends Application {

   Media video_source;
   MediaPlayer video_mp;
   MediaView video_mv;

   @Override
   public void start(Stage primaryStage) {
       Pane root1 = new Pane();
       BorderPane videoPane = new BorderPane();
       Scene vidScene = new Scene(videoPane, 1280, 800);
       Scene mainScene = new Scene(root1, 1280, 800);

       MenuBar video_menuBar = new MenuBar();
       Menu video_menuFile = new Menu("File");

       MenuItem video_open = new MenuItem("Open File...");
       video_open.setOnAction((ActionEvent t) -> {
           File file = new FileChooser().showOpenDialog(primaryStage);
           if (file != null) {
               video_source = new Media(file.toURI().toString());
               video_mv = new MediaView(video_mp);
           }
       });
       video_menuFile.getItems().addAll(video_open);

       Menu video_menuEdit = new Menu("Edit");

       Menu video_menuView = new Menu("View");
       MenuItem mainPage = new MenuItem("Main Menu");
       mainPage.setOnAction(e -> primaryStage.setScene(mainScene));
       video_menuView.getItems().add(mainPage);

       video_menuBar.getMenus().addAll(video_menuFile, video_menuEdit, video_menuView);
       video_menuBar.prefWidthProperty().bind(videoPane.widthProperty());

       videoPane.setTop(video_menuBar);
       StackPane stack = new StackPane();
       stack.getChildren().addAll(new Rectangle(1000, 600, Color.BLACK), video_mv);
       videoPane.setCenter(stack);
       video_mp.play();

       primaryStage.setScene(mainScene);

       primaryStage.setTitle("Media Player");
       primaryStage.show();
   }

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

我还要注意,即使没有视频播放,我也想要一个黑色矩形。

1 个答案:

答案 0 :(得分:1)

您的代码实际上存在一些问题。简而言之,在setOnAction中,您应该从所选视频文件中创建MediaPlayer,然后将其设置为MediaView的播放器,然后播放。

以下是工作示例:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.FileChooser;
import javafx.stage.Stage;

import java.io.File;

public class Video extends Application {

    @Override
    public void start(Stage primaryStage) {
        Pane root = new Pane();
        BorderPane videoPane = new BorderPane();
        Scene mainScene = new Scene(root, 1280, 800);

        MenuBar video_menuBar = new MenuBar();
        Menu video_menuFile = new Menu("File");

        MediaView video_mv = new MediaView();

        MenuItem video_open = new MenuItem("Open File...");
        video_open.setOnAction(t -> {
            FileChooser chooser = new FileChooser();
            File file = chooser.showOpenDialog(primaryStage);
            if (file != null) {
                Media video_source = new Media(file.toURI().toString());
                MediaPlayer video_mp = new MediaPlayer(video_source);
                video_mv.setMediaPlayer(video_mp);
                video_mp.play();
            }
        });
        video_menuFile.getItems().addAll(video_open);

        Menu video_menuEdit = new Menu("Edit");

        Menu video_menuView = new Menu("View");
        MenuItem mainPage = new MenuItem("Main Menu");
        mainPage.setOnAction(e -> primaryStage.setScene(mainScene));
        video_menuView.getItems().add(mainPage);

        video_menuBar.getMenus().addAll(video_menuFile, video_menuEdit, video_menuView);
        video_menuBar.prefWidthProperty().bind(videoPane.widthProperty());

        videoPane.setTop(video_menuBar);
        StackPane stack = new StackPane();
        stack.getChildren().addAll(new Rectangle(1000, 600, Color.BLACK), video_mv);
        videoPane.setCenter(stack);

        root.getChildren().add(videoPane);

        primaryStage.setScene(mainScene);

        primaryStage.setTitle("Media Player");
        primaryStage.show();
    }

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

}

请注意,某些视频格式为are not supported