简体   繁体   English

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

[英]How do I play media in a Stack Pane in Javafx?

I'm learning how to use JavaFx and I am trying to create a media player with two scenes where one plays videos and the other plays music. 我正在学习如何使用JavaFx,并且试图创建一个具有两个场景的媒体播放器,其中一个场景播放视频,另一个场景播放音乐。 I am working on the video scene right now and cannot figure out why my code isn't working. 我现在正在视频场景上工作,无法弄清为什么我的代码无法正常工作。

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);
   }
}

I would also like to note, that I want a black rectangle present even when there isn't a video playing. 我还要指出的是,即使没有视频播放,我也希望有一个黑色矩形。

There are actually several problems in your code. 您的代码中实际上存在几个问题。 In short, in setOnAction you should create the MediaPlayer from the chosen video file, then set it as a player for MediaView and then play it. 简而言之,在setOnAction您应该从所选的视频文件创建MediaPlayer ,然后将其设置为MediaView的播放器,然后播放。

Here is the working example: 这是工作示例:

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);
    }

}

Note, that some video formats are not supported . 请注意,某些视频格式不受支持

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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