Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8230887

JavaFX MediaPlayer: plays half as many times it should when setStopTime is used

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: P4 P4
    • tbd
    • jfx11, jfx13
    • javafx
    • x86_64
    • windows_10

      ADDITIONAL SYSTEM INFORMATION :
      Tested on Windows 10 with Java 10 and Java 12.
      Bug occurs on JavaFX 10, 11, 13 (other versions not tested)

      A DESCRIPTION OF THE PROBLEM :
      When setStopTime is used on MediaPlayer, some medias plays about half as many times as is specified by setCycleCount.

      So if I use player.setCycleCount(10), the sound plays only about 5 times instead of 10.

      I did more tests and it seems that after playing once, currentCount is increased by 2 instead of 1, and OnEndOfMedia is triggered twice too.

      The problem seems to be limited to some of the medias. An example is the file https://ia601701.us.archive.org/21/items/Laser_201304/laser.mp3 (about 1.8 seconds long)

      The problem disappear if I stop using setStopTime.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1. Run the source code
      2. Count the number of times the laser sound is played.
      3. Look at the console output.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The laser sound is played 10 times, and each time it plays a single line is printed to the console.
      ACTUAL -
      The sound is played 5-8 times instead of 10.
      On some plays, two lines are printed instead of one.

      Example of output:

      Current Count: 1 Seconds: 3.456
      Current Count: 2 Seconds: 3.465
      Current Count: 3 Seconds: 5.334
      Current Count: 4 Seconds: 7.177
      Current Count: 5 Seconds: 7.185
      Current Count: 6 Seconds: 9.056
      Current Count: 7 Seconds: 10.896
      Current Count: 8 Seconds: 10.906
      Current Count: 9 Seconds: 12.776
      Current Count: 10 Seconds: 12.798

      ---------- BEGIN SOURCE ----------
      package main;

      import javafx.application.Application;
      import javafx.scene.Group;
      import javafx.scene.Scene;
      import javafx.scene.media.Media;
      import javafx.scene.media.MediaPlayer;
      import javafx.scene.media.MediaView;
      import javafx.stage.Stage;
      import javafx.util.Duration;

      public class Main extends Application {
          @Override
          public void start(Stage primaryStage) {
              Media media = new Media("https://ia601701.us.archive.org/21/items/Laser_201304/laser.mp3"); // replace this with your own audio file
              MediaPlayer player = new MediaPlayer(media);

              // Add to scene
              Group root = new Group(new MediaView(player));
              Scene scene = new Scene(root, 500, 200);

              // Show the stage
              primaryStage.setTitle("Media Player");
              primaryStage.setScene(scene);
              primaryStage.show();

              player.setCycleCount(10); // should run 10 times, but runs 5-8 times
              player.setStopTime(new Duration(1800)); // removing this fixes the issue

              long start = System.currentTimeMillis();
              player.setOnEndOfMedia(
                      new Runnable() {
                          @Override
                          public void run() {
                              float sec = (System.currentTimeMillis() - start) / 1000F;
                              System.out.println("Current Count: " + player.getCurrentCount() + " Seconds: " + sec);
                          }
                      });
              player.play();
          }
          public static void main(String[] args) {
              launch(args);
          }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Removing the line "player.setStopTime(new Duration(1800));" fixes the issue.

      FREQUENCY : always


            almatvee Alexander Matveev
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated: