title: Javafx FXML Media Player play same Video in two windows simultaneously
body: <p>im have a simple Media Player created in Java with Javafx FXML.</p>
  
  <p>Now what i want to achieve, is to play the video also in a second window, sadly ive got no clue how this is done. I searched other posts about the same question but found nothing what would help me.</p>
  
  <p>Here's the code</p>
  
  <p>simplemediaPlayer.java :</p>
  
  <pre><code>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&lt;MouseEvent&gt;() {
          @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);
  }
  
  }
  </code></pre>
  
  <p>FXMLDocumentController.java :</p>
  
  <pre><code>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&lt;Duration&gt;() {
              @Override
              public void changed(ObservableValue&lt;? extends Duration&gt; observable, Duration oldValue, Duration newValue) {
                  minSlider.setValue(newValue.toSeconds());
              }
          });
  
          minSlider.setOnMouseClicked(new EventHandler&lt;MouseEvent&gt;() {
              @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) {
  
  }    
  
  }
  </code></pre>
  
  <p>FXMLDocument.fxml :</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  
  &lt;?import javafx.geometry.Insets?&gt;
  &lt;?import javafx.scene.control.Button?&gt;
  &lt;?import javafx.scene.control.Menu?&gt;
  &lt;?import javafx.scene.control.MenuBar?&gt;
  &lt;?import javafx.scene.control.MenuItem?&gt;
  &lt;?import javafx.scene.control.Slider?&gt;
  &lt;?import javafx.scene.layout.BorderPane?&gt;
  &lt;?import javafx.scene.layout.HBox?&gt;
  &lt;?import javafx.scene.layout.StackPane?&gt;
  &lt;?import javafx.scene.layout.VBox?&gt;
  &lt;?import javafx.scene.media.MediaView?&gt;
  
   &lt;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"&gt;
    &lt;children&gt;
    &lt;MediaView fx:id="mediaView" fitHeight="200.0" fitWidth="200.0" /&gt;
    &lt;BorderPane prefHeight="200.0" prefWidth="200.0"&gt;
       &lt;top&gt;
          &lt;MenuBar fx:id="topMenu" BorderPane.alignment="CENTER"&gt;
            &lt;menus&gt;
              &lt;Menu mnemonicParsing="false" text="File"&gt;
                &lt;items&gt;
                  &lt;MenuItem mnemonicParsing="false" onAction="#openvidFile" text="Open" /&gt;
                      &lt;MenuItem mnemonicParsing="false" onAction="#exitProg" text="Close" /&gt;
                &lt;/items&gt;
              &lt;/Menu&gt;
              &lt;Menu mnemonicParsing="false" text="Edit"&gt;
                &lt;items&gt;
                  &lt;MenuItem mnemonicParsing="false" text="Delete" /&gt;
                &lt;/items&gt;
              &lt;/Menu&gt;
              &lt;Menu mnemonicParsing="false" text="Help"&gt;
                &lt;items&gt;
                  &lt;MenuItem mnemonicParsing="false" text="About" /&gt;
                &lt;/items&gt;
              &lt;/Menu&gt;
            &lt;/menus&gt;
          &lt;/MenuBar&gt;
       &lt;/top&gt;
       &lt;bottom&gt;
          &lt;VBox fx:id="botMenu" alignment="BOTTOM_CENTER" prefHeight="84.0" prefWidth="600.0" BorderPane.alignment="CENTER"&gt;
             &lt;children&gt;
                &lt;Slider fx:id="minSlider" /&gt;
                &lt;HBox alignment="CENTER" prefHeight="57.0" prefWidth="600.0"&gt;
                   &lt;children&gt;
                      &lt;Button fx:id="openClick" mnemonicParsing="false" onAction="#openvidFile" text="Open"&gt;
                         &lt;HBox.margin&gt;
                            &lt;Insets right="10.0" /&gt;
                         &lt;/HBox.margin&gt;
                      &lt;/Button&gt;
                      &lt;Button fx:id="playClick" mnemonicParsing="false" onAction="#playVid" text="Play"&gt;
                         &lt;HBox.margin&gt;
                            &lt;Insets right="10.0" /&gt;
                         &lt;/HBox.margin&gt;
                      &lt;/Button&gt;
                      &lt;Button fx:id="pauseClick" mnemonicParsing="false" onAction="#pauseVid" text="Pause"&gt;
                         &lt;HBox.margin&gt;
                            &lt;Insets right="20.0" /&gt;
                         &lt;/HBox.margin&gt;
                      &lt;/Button&gt;
                      &lt;Slider fx:id="volSlider" prefHeight="25.0" prefWidth="140.0" /&gt;
                      &lt;Button fx:id="dualmodeButton" mnemonicParsing="false" text="Dual Mode"&gt;
                         &lt;HBox.margin&gt;
                            &lt;Insets left="30.0" /&gt;
                         &lt;/HBox.margin&gt;
                      &lt;/Button&gt;
                   &lt;/children&gt;
                &lt;/HBox&gt;
             &lt;/children&gt;
          &lt;/VBox&gt;
       &lt;/bottom&gt;
    &lt;/BorderPane&gt;
   &lt;/children&gt;
   &lt;/StackPane&gt;
  </code></pre>
  
  <p>and i created a second fxml window which only contains a stackpane and a mediaview.</p>
  
  <p>dualmodeWindow.fxml :</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  
  &lt;?import javafx.scene.layout.StackPane?&gt;
  &lt;?import javafx.scene.media.MediaView?&gt;
  
  &lt;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"&gt;
  &lt;children&gt;
    &lt;MediaView fx:id="secmediaView" fitHeight="200.0" fitWidth="200.0" /&gt;
  &lt;/children&gt;
  &lt;/StackPane&gt;
  </code></pre>
  
  <p>Ive tried to give the mediaView on the second window the same id as the mediaView on the main Window but no video appear on the second window.</p>
  
  <p>Would be nice if someone can help me with this. </p>