Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
DarioArena87 opened this issue Mar 14, 2023 · 0 comments
Open

Comments

@DarioArena87
Copy link

DarioArena87 commented Mar 14, 2023

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();
        }
    }
}
`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant