简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM