JavaFX throws ERROR_MEDIA_INVALID when playing certain mp3 files

Viewed 240

I keep getting an ERROR_MEDIA_INVALID error when trying to play some .mp3 files via the JavaFX media player. Previous google searches have just led to old bug reports, and I'm not sure if this an actual bug or something I missed.

Example:

package sample;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.stage.Stage;

import java.io.File;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{

        primaryStage.setTitle("Hello World!");
        Button btn = new Button();
        btn.setText("Say 'Hello World'");
        btn.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {

                //change filename as needed
                var file = new File("C:/Users/tgaravaglia/Downloads/test.mp3");

                var media = new Media(file.toURI().toString());
                var player = new MediaPlayer(media);
                player.setOnError(new Runnable()
                {
                    @Override
                    public void run()
                    {
                        //ERROR_MEDIA_INVALID here
                        player.getError().printStackTrace();
                    }
                });
                player.play();
            }
        });

        StackPane root = new StackPane();
        root.getChildren().add(btn);
        primaryStage.setScene(new Scene(root, 300, 250));
        primaryStage.show();
    }


    public static void main(String[] args) {
        launch(args);
    }
}

Stack trace:

MediaException: UNKNOWN : [com.sun.media.jfxmediaimpl.platform.gstreamer.GSTMediaPlayer@6f4b481f] ERROR_MEDIA_INVALID: ERROR_MEDIA_INVALID
    at javafx.media/javafx.scene.media.MediaException.getMediaException(MediaException.java:160)
    at javafx.media/javafx.scene.media.MediaPlayer$_MediaErrorListener.onError(MediaPlayer.java:2623)
    at javafx.media/com.sun.media.jfxmediaimpl.NativeMediaPlayer$EventQueueThread.HandleErrorEvents(NativeMediaPlayer.java:692)
    at javafx.media/com.sun.media.jfxmediaimpl.NativeMediaPlayer$EventQueueThread.run(NativeMediaPlayer.java:426)

Working file: https://drive.google.com/file/d/1e3k9gVhV_hDehWHwhHElXC2jU4aKn6oc/view?usp=sharing

Broken file: https://drive.google.com/file/d/1_VKx4zLH6lFLv6VJdDvrP6IP3c-TjOYo/view?usp=sharing

1 Answers

So... I figured it out. That bad "mp3" file is actually a .wav file in disguise. I ran it through a cloud-based wav->mp3 converter and now it works fine. Hopefully this helps someone in the future with the same problem!

Related