title: How to wait for JavaFx MediaPlayer to finish
body: <p>I have a <code>MediaPlayer</code> object which plays a video. When the video is finished playing, I want the program to hide the <code>MediaView</code> and display different JavaFx entities. However, when I call <code>player.play()</code> the program immediately interprets the lines of code which follow, resulting in the <code>MediaView</code> not being visible at all. </p>
  
  <pre><code>public class Level1Controller implements Initializable {
  
      File file = new File("ngnl.mp4");
      Media media = new Media(file.toURI().toString());
      MediaPlayer player = new MediaPlayer(media);
      @FXML
      MediaView view = new MediaView();
  
  
      /**
       * Initializes the controller class.
       */
  
      @Override
      public void initialize(URL url, ResourceBundle rb) {
  
          view.setMediaPlayer(player);
          player.play();
  
          view.setVisible(false); 
      }
  }
  </code></pre>
  
  <p>More buttons and such would be "under" the <code>MediaView</code> in the JavaFx hierarchy, so when it is hidden, the underlying stuff would be seen.</p>