I'm trying to play an mp3 file using the JavaFX MediaPlayer. It loads the file and switches to the PLAYING state without any errors, but it doesn't play the file and the currentTimeProperty doesn't change either. What am I doing wrong?
public class Test extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
MediaPlayer player = new MediaPlayer(new Media(
new File("sounds/sound.mp3").toURI().toString()
));
Button btn = new Button("Play");
btn.setOnAction(event -> player.play());
VBox pane = new VBox(10, btn);
pane.setAlignment(Pos.CENTER);
Scene scene = new Scene(pane, 100, 100);
primaryStage.setScene(scene);
primaryStage.show();
}
}
The code you have provide works fine as it is for me.I am not an expert with MediaPlayer and MediaView classes but assuming you are loading the media correctly and not getting a MediaException: MEDIA_UNAVAILABLE
while you load the mp3 file, there are two possible causes for your problem.
player.setOnError(() -> System.out.println("Error : " + player.getError().toString()));