Aboud Zakaria
Aboud Zakaria

Reputation: 567

JavaFX MediaPlayer status stuck on PLAYING

public void play(File audioFile, Double startMillisecond, Double stopMillisecond) {
    if (mediaPlayer != null) {
        mediaPlayer.stop();
        mediaPlayer.dispose();
    }
    String bip = audioFile.toURI().toString();
    Media hit = new Media(bip);
    mediaPlayer = new MediaPlayer(hit);
    if (startMillisecond != null)
        mediaPlayer.setStartTime(Duration.millis(startMillisecond));
    if (stopMillisecond != null)
        mediaPlayer.setStopTime(Duration.millis(stopMillisecond));
    mediaPlayer.play();
}

When playing a sound file in a specific duration, mediaPlayer.getStatus() stuck on "PLAYING" forever even after the playback stops at the given StopTime

Upvotes: 0

Views: 1281

Answers (2)

Guillaume F.
Guillaume F.

Reputation: 1130

The following is like Aboud Zakaria's answer, but works when trying to play media more than once:

mediaPlayer.setOnEndOfMedia(
    new Runnable() {
        @Override
        public void run() {
            int curCount = mediaPlayer.getCurrentCount();
            int cycCount = mediaPlayer.getCycleCount();
            if (cycCount != MediaPlayer.INDEFINITE && curCount >= cycCount)
                mediaPlayer.stop();
        }
    });
};

Upvotes: 0

Aboud Zakaria
Aboud Zakaria

Reputation: 567

a workaround for this issue usingOnEndOfMedia Event

mediaPlayer.setOnEndOfMedia(new Runnable() {
    @Override
    public void run() {
        mediaPlayer.stop();
    }
});

now mediaPlayer.getStatus() returns "STOPPED" when playback ends.

Upvotes: 1

Related Questions