【问题标题】:Javafx FXML Media Player play same Video in two windows simultaneouslyJavafx FXML 媒体播放器同时在两个窗口中播放相同的视频
【发布时间】:2017-05-11 12:57:25
【问题描述】:

我有一个使用 Javafx FXML 用 Ja​​va 创建的简单媒体播放器。

现在我想要实现的是在第二个窗口中播放视频,遗憾的是我不知道这是如何完成的。我搜索了有关同一问题的其他帖子,但没有找到任何对我有帮助的帖子。

这是代码

simplemediaPlayer.java :

package simplemediaplayer;

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;
import javafx.stage.Stage;

public class simplemediaPlayer extends Application {

@Override
public void start(Stage stage) throws Exception {
    Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));

    Scene scene = new Scene(root);

    stage.setTitle("Media Player");
    scene.setOnMouseClicked(new EventHandler<MouseEvent>() {
        @Override
        public void handle(MouseEvent doubleClicked) {
            if(doubleClicked.getClickCount() == 2) {
                if(stage.isFullScreen()) {
                    stage.setFullScreen(false);
                } else {
                    stage.setFullScreen(true);
                }
            }
        }
    });

    stage.setScene(scene);
    stage.show();

    Parent roota = FXMLLoader.load(getClass().getResource("dualmodeWindow.fxml"));
    Stage stagea = new Stage();
    Scene scenea = new Scene(roota);

    stagea.setTitle("Second Window");

    stagea.setScene(scenea);
    stagea.show();

}

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

}

FXMLDocumentController.java:

package simplemediaplayer;

import java.io.File;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.beans.InvalidationListener;
import javafx.beans.Observable;
import javafx.beans.binding.Bindings;
import javafx.beans.property.DoubleProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Slider;
import javafx.scene.input.MouseEvent;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import javafx.stage.FileChooser;
import javafx.util.Duration;

public class FXMLDocumentController implements Initializable {

private MediaPlayer mediaPlayer;

@FXML
private MediaView mediaView;

private String filePath;

@FXML
private Slider volSlider;

@FXML
private Slider minSlider;

@FXML
private void openvidFile(ActionEvent event) {
    FileChooser fileChooser = new FileChooser();
    FileChooser.ExtensionFilter filter = new FileChooser.ExtensionFilter("Select a File (*.mp4)", "*.mp4");
    fileChooser.getExtensionFilters().add(filter);
    File file = fileChooser.showOpenDialog(null);
    filePath = file.toURI().toString();

    if(filePath != null) {
        Media media = new Media(filePath);
        mediaPlayer = new MediaPlayer(media);
        mediaView.setMediaPlayer(mediaPlayer);
        DoubleProperty width = mediaView.fitWidthProperty();
        DoubleProperty height = mediaView.fitHeightProperty();

        width.bind(Bindings.selectDouble(mediaView.sceneProperty(), "width"));
        height.bind(Bindings.selectDouble(mediaView.sceneProperty(), "height"));

        volSlider.setValue(mediaPlayer.getVolume() * 100);
        volSlider.valueProperty().addListener(new InvalidationListener() {
            @Override
            public void invalidated(Observable observable) {
                mediaPlayer.setVolume(volSlider.getValue()/100);
            }
        });

        mediaPlayer.currentTimeProperty().addListener(new ChangeListener<Duration>() {
            @Override
            public void changed(ObservableValue<? extends Duration> observable, Duration oldValue, Duration newValue) {
                minSlider.setValue(newValue.toSeconds());
            }
        });

        minSlider.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                mediaPlayer.seek(Duration.seconds(minSlider.getValue()));
            }
        });

        mediaPlayer.play();
    }
}

@FXML
private void playVid(ActionEvent event) {
    mediaPlayer.play();
    mediaPlayer.setRate(1);
}

@FXML
private void pauseVid(ActionEvent event) {
    mediaPlayer.pause();
}

@FXML
private void stopVid(ActionEvent event) {
    mediaPlayer.stop();
}

@FXML
private void exitProg(ActionEvent event) {
    System.exit(0);
}
@Override
public void initialize(URL url, ResourceBundle rb) {

}    

}

FXMLDocument.fxml:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.control.Slider?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.StackPane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.media.MediaView?>

 <StackPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" stylesheets="@style.css" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="simplemediaplayer.FXMLDocumentController">
  <children>
  <MediaView fx:id="mediaView" fitHeight="200.0" fitWidth="200.0" />
  <BorderPane prefHeight="200.0" prefWidth="200.0">
     <top>
        <MenuBar fx:id="topMenu" BorderPane.alignment="CENTER">
          <menus>
            <Menu mnemonicParsing="false" text="File">
              <items>
                <MenuItem mnemonicParsing="false" onAction="#openvidFile" text="Open" />
                    <MenuItem mnemonicParsing="false" onAction="#exitProg" text="Close" />
              </items>
            </Menu>
            <Menu mnemonicParsing="false" text="Edit">
              <items>
                <MenuItem mnemonicParsing="false" text="Delete" />
              </items>
            </Menu>
            <Menu mnemonicParsing="false" text="Help">
              <items>
                <MenuItem mnemonicParsing="false" text="About" />
              </items>
            </Menu>
          </menus>
        </MenuBar>
     </top>
     <bottom>
        <VBox fx:id="botMenu" alignment="BOTTOM_CENTER" prefHeight="84.0" prefWidth="600.0" BorderPane.alignment="CENTER">
           <children>
              <Slider fx:id="minSlider" />
              <HBox alignment="CENTER" prefHeight="57.0" prefWidth="600.0">
                 <children>
                    <Button fx:id="openClick" mnemonicParsing="false" onAction="#openvidFile" text="Open">
                       <HBox.margin>
                          <Insets right="10.0" />
                       </HBox.margin>
                    </Button>
                    <Button fx:id="playClick" mnemonicParsing="false" onAction="#playVid" text="Play">
                       <HBox.margin>
                          <Insets right="10.0" />
                       </HBox.margin>
                    </Button>
                    <Button fx:id="pauseClick" mnemonicParsing="false" onAction="#pauseVid" text="Pause">
                       <HBox.margin>
                          <Insets right="20.0" />
                       </HBox.margin>
                    </Button>
                    <Slider fx:id="volSlider" prefHeight="25.0" prefWidth="140.0" />
                    <Button fx:id="dualmodeButton" mnemonicParsing="false" text="Dual Mode">
                       <HBox.margin>
                          <Insets left="30.0" />
                       </HBox.margin>
                    </Button>
                 </children>
              </HBox>
           </children>
        </VBox>
     </bottom>
  </BorderPane>
 </children>
 </StackPane>

我创建了第二个 fxml 窗口,它只包含一个堆栈窗格和一个媒体视图。

双模式窗口.fxml:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.layout.StackPane?>
<?import javafx.scene.media.MediaView?>

<StackPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="simplemediaplayer.FXMLDocumentController">
<children>
  <MediaView fx:id="secmediaView" fitHeight="200.0" fitWidth="200.0" />
</children>
</StackPane>

我尝试为第二个窗口上的 mediaView 赋予与主窗口上的 mediaView 相同的 ID,但第二个窗口上没有出现视频。

如果有人可以帮助我,那就太好了。

【问题讨论】:

  • 您需要找到一种方法在两个 MediaView 实例之间共享同一个 MediaPlayer。
  • 是的,好的,我现在尝试将第二个 mediaView 设置为相同的 MediaPlayer,但是当我这样做时,MediaPlayer 不再工作了。如何在两个 MediaView 之间共享同一个 MediaPlayer?
  • 如果您尝试了某些方法但它不起作用,请更新您的问题。此外,为简单起见,请尝试先在没有 FXML 和控制器的情况下对其进行测试。这也将允许您生成一个MCVE,您可以将其发布在问题中。

标签: javafx media-player fxml


【解决方案1】:

您需要使用相同的媒体对象创建第二个媒体播放器,然后将此第二个媒体播放器设置为第二个媒体视图。根据文档,媒体播放器只能有一个媒体对象。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-27
    • 1970-01-01
    相关资源
    最近更新 更多