Christopher Smith
Christopher Smith

Reputation: 450

JavaFX play mp3

When I run my method, I get a MediaException. I call the method with playSound("src/assets/timeup.mp3");.

private void playSound(String path) {
        System.out.println(path);
        Media hit = new Media(new File(path).toURI().toString());
        System.out.println(hit.getSource());
        MediaPlayer mediaPlayer = new MediaPlayer(hit);
        mediaPlayer.play();
    }

Log:

src/assets/timeup.mp3
file:/media/chris/1%20TB%20Data/Eclipse/workspace/DrEggTimer/src/assets/timeup.mp3
Exception in thread "main" MediaException: UNKNOWN : com.sun.media.jfxmedia.MediaException: Could not create player! : com.sun.media.jfxmedia.MediaException: Could not create player!
    at javafx.scene.media.MediaException.exceptionToMediaException(MediaException.java:146)
    at javafx.scene.media.MediaPlayer.init(MediaPlayer.java:511)
    at javafx.scene.media.MediaPlayer.<init>(MediaPlayer.java:414)
    at timer.Timer.playSound(Timer.java:53)
    at timer.Timer.<init>(Timer.java:58)
    at timer.Timer.main(Timer.java:39)
Caused by: com.sun.media.jfxmedia.MediaException: Could not create player!
    at com.sun.media.jfxmediaimpl.NativeMediaManager.getPlayer(NativeMediaManager.java:222)
    at com.sun.media.jfxmedia.MediaManager.getPlayer(MediaManager.java:104)
    at javafx.scene.media.MediaPlayer.init(MediaPlayer.java:467)
    ... 4 more

My project is on an external drive and I am linking it to my main drive with a symlink. I am running Eclipse on the main drive. I don't believe that should cause any problems though.

Upvotes: 1

Views: 597

Answers (1)

keirebu
keirebu

Reputation: 337

If you are on linux (which it appears you are from your stacktrace). You need to install libavformat53 and libavcodec53 for javafx's MediaPlayer to work, else you will get that error.

Upvotes: 2

Related Questions