JavaFX播放mp3

时间:2014-11-08 23:22:22

标签: java javafx mp3

当我运行我的方法时,我得到MediaException。我用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();
    }

日志:

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

我的项目位于外部驱动器上,我使用符号链接将其链接到主驱动器。我在主驱动器上运行Eclipse。我不认为这会导致任何问题。

1 个答案:

答案 0 :(得分:2)

如果您使用的是Linux(看起来您来自堆栈跟踪)。您需要为javafx的MediaPlayer安装libavformat53和libavcodec53才能工作,否则您将收到该错误。