gpt4 book ai didi

java - 如何在 Javafx 的堆栈 Pane 中播放媒体?

转载 作者:行者123 更新时间:2023-12-01 08:54:50 25 4
gpt4 key购买 nike

我正在学习如何使用 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);
}
}

我还想指出,即使没有播放视频,我也希望出现一个黑色矩形。

最佳答案

您的代码实际上存在几个问题。简而言之,在 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 .

关于java - 如何在 Javafx 的堆栈 Pane 中播放媒体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42102001/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com