Aboud Zakaria Aboud Zakaria - 5 months ago 40
Java Question

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

Answer

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.