Skip to content

Issue in playing audio file using javafx.scene.media.MediaPlayer #71

@DarioArena87

Description

@DarioArena87

Hi, i think i found an issue when playing audio file using javafx.scene.media.MediaPlayer. I am trying to build a "toy" media player from which the user can choose an audio file and then play it. The first playback is fine then if the user clicks on the "pause" button the audio stops, then if i resume the audio playback using mediaPlayer.play() the audio starts, it plays for about 100ms, then pauses for about another 100ms and then starts again playing normally.
I am on a linux system and i tried using an .mp3 file and a .wav file. Using the javafx gradle plugin i tried with OpenJavaFx version 17.02, 17.06 and 19. Am i maybe doing something wrong? The code that i am using is the following:

package it.arena.audioplayer.audioplayer;

import javafx.beans.binding.Bindings;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.stage.FileChooser;

class AudioPlayerController {
    @FXML
    private Label statusLabel;

    @FXML
    private Button openButton;

    @FXML
    private Button playPauseButton;

    private SimpleBooleanProperty fileChosen = new SimpleBooleanProperty(false);
    private SimpleBooleanProperty playing = new SimpleBooleanProperty(false);

    private MediaPlayer mediaPlayer;

    public void initialize() {
        statusLabel.textProperty().bind(Bindings.when(playing).then('Playing').otherwise('Stopped'));
        playPauseButton.disableProperty().bind(fileChosen.not());
    }

    @FXML
    public void onOpenButtonClicked() {
        FileChooser fileChooser = new FileChooser();
        fileChooser.setTitle("Open File");
        File file = fileChooser.showOpenDialog(statusLabel.getScene().getWindow());
        URI uri = file.toURI().toString();
        mediaPlayer = new MediaPlayer(new Media(uri));
        fileChosen.set(true);
    }

    @FXML
    public void onPlayPauseClick() {
        playing.set(!playing.get());
        if (playing.get()) {
            mediaPlayer.play();
        }
        else {
            mediaPlayer.pause();
        }
    }
}
`

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions