示例#1
0
 @Override
 public void setMediaStoppedListener(MediaEventListener listener) {
   log.log(LogType.PLAYER, "setMediaStoppedListener");
   player.setOnStopped(listener::mediaEvent);
 }
示例#2
0
  /*
   * Function that creates a play button, adds the relevant event handlers for it,
   * and adds it to the appropiate media control bar. Done for both normal view mode
   * and fullscreen mode.
   */
  void setPlayButton() {
    // Create a play button.
    playButton = new Button();
    playButton.setGraphic(new ImageView(playImage));
    playButton.setOnAction(
        new EventHandler<ActionEvent>() {

          // ActionHandler for play button.
          public void handle(ActionEvent e) {
            updateValues();
            Status status = mp.getStatus();

            // Check for bad status's.
            if (status == Status.UNKNOWN || status == Status.HALTED) {
              System.out.println("Player is in a bad or unknown state, can't play.");
              return;
            }

            // Check for accepted status's.
            if (status == Status.PAUSED || status == Status.READY || status == Status.STOPPED) {
              // Rewind the video if it's at the end
              if (atEndOfMedia) {
                mp.seek(mp.getStartTime());
                atEndOfMedia = false;
                playButton.setGraphic(new ImageView(playImage));
                updateValues();
              }
              // Set video to play again and set pause image for button
              mp.play();
              playButton.setGraphic(new ImageView(pauseImage));
            } else {
              // Pause the media and set play image for button
              mp.pause();
              playButton.setGraphic(new ImageView(playImage));
            }
          }
        });

    // Play/Pause Button in fullscreen mode
    playButtonFS = new Button();
    playButtonFS.setGraphic(new ImageView(playImage));
    playButtonFS.setOnAction(
        new EventHandler<ActionEvent>() {

          // ActionHandler for play button in fullscreen mode.
          public void handle(ActionEvent e) {
            updateValues();
            Status status = mp.getStatus();

            // Check for bad status's
            if (status == Status.UNKNOWN || status == Status.HALTED) {
              System.out.println("Player is in a bad or unknown state, can't play.");
              return;
            }

            // Check for acceptable status's
            if (status == Status.PAUSED || status == Status.READY || status == Status.STOPPED) {
              // rewind the movie if we're sitting at the end
              if (atEndOfMedia) {
                mp.seek(mp.getStartTime());
                atEndOfMedia = false;
                playButtonFS.setGraphic(new ImageView(playImage));
                updateValues();
              }
              // Set video to play again and set pause image for button
              mp.play();
              playButtonFS.setGraphic(new ImageView(pauseImage));
            } else {
              // Pause the media and set play image for button
              mp.pause();
              playButtonFS.setGraphic(new ImageView(playImage));
            }
          }
        });

    // Whenever there's a change in duration of the MediaPlayer, update the Time Label and Slider
    // Position
    mp.currentTimeProperty()
        .addListener(
            new ChangeListener<Duration>() {
              @Override
              public void changed(
                  ObservableValue<? extends Duration> observableValue,
                  Duration duration,
                  Duration current) {
                updateValues();
              }
            });

    // Media is playing so set appropiate playButton image
    mp.setOnPlaying(
        new Runnable() {

          public void run() {
            if (stopRequested) {
              // If a media stop has been requested in the meantime, pause the media.
              mp.pause();
              stopRequested = false;
            } else {
              // Otherwise set the pause image for the "play" button
              playButton.setGraphic(new ImageView(pauseImage));
              playButtonFS.setGraphic(new ImageView(pauseImage));
            }
          }
        });

    // Media is paused so set appropiate playButton image
    mp.setOnPaused(
        new Runnable() {

          public void run() {
            // Set the play image for the "play" button
            playButton.setGraphic(new ImageView(playImage));
            playButtonFS.setGraphic(new ImageView(playImage));
          }
        });

    // Media is stopped so set appropiate playButton image
    mp.setOnStopped(
        new Runnable() {

          public void run() {
            // Media has stopped so display the play image for the "play" button
            mp.setStopTime(Duration.INDEFINITE);
            atEndOfMedia = true;
            playButton.setGraphic(new ImageView(playImage));
            playButtonFS.setGraphic(new ImageView(playImage));
          }
        });

    // Media is ready so get the total duration of the Media and update the Time Label
    mp.setOnReady(
        new Runnable() {

          public void run() {
            duration = mp.getMedia().getDuration();
            updateValues();
          }
        });

    // Media has finished playing so set appropiate playButton image & handle loops
    mp.setOnEndOfMedia(
        new Runnable() {

          public void run() {
            if (!mpLoop) {
              // If loop not set then stop media and display play image for "play" button
              playButton.setGraphic(new ImageView(playImage));
              playButtonFS.setGraphic(new ImageView(playImage));
              atEndOfMedia = true;
              mp.stop();
            }

            // Otherwise go back to the requested start time of the media
            mp.seek(mp.getStartTime());
          }
        });

    // Media is repeating so set appropiate playButton image
    mp.setOnRepeat(
        new Runnable() {

          public void run() {
            // Display the pause image for the "play" button
            atEndOfMedia = false;
            playButton.setGraphic(new ImageView(pauseImage));
            playButtonFS.setGraphic(new ImageView(pauseImage));
          }
        });

    // Add the play button to the MediaControl bar.
    mediaBar.getChildren().add(playButton);
  }