简体   繁体   English

JavaFX播放mp3

[英]JavaFX play mp3

When I run my method, I get a MediaException . 当我运行我的方法时,我得到一个MediaException I call the method with playSound("src/assets/timeup.mp3"); 我用playSound("src/assets/timeup.mp3");调用该方法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. 我在主驱动器上运行Eclipse。 I don't believe that should cause any problems though. 我认为这不会引起任何问题。

If you are on linux (which it appears you are from your stacktrace). 如果您使用的是Linux(看起来好像是您的stacktrace)。 You need to install libavformat53 and libavcodec53 for javafx's MediaPlayer to work, else you will get that error. 您需要安装libavformat53和libavcodec53才能使javafx的MediaPlayer正常工作,否则会出现该错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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