简体   繁体   English

Javafx 无法在 ubuntu 上播放带有 MediaException 的 mp3 文件 20

[英]Javafx can't play mp3 file with MediaException on ubuntu 20

  1. configuration: ubuntu 20, javafx 11, jdk15, language level 8配置:ubuntu 20,javafx 11,jdk15,语言8级
  2. problem: mp3 file could be directly play on ubuntu, while can't be played in following code, but the code can be used to play *.wav and *.aiff audio files, except mp3:问题:在ubuntu上可以直接播放mp3文件,但是用下面的代码不能播放,但是用这段代码可以播放*.wav和*.aiff音频文件,除了mp3:
    Media media = new Media(new File("assets/audios/shoot.mp3").toURI().toString());
        MediaPlayer mediaPlayer = new MediaPlayer(media);
        mediaPlayer.play();
  1. exception:例外:
    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:518)
    at javafx.scene.media.MediaPlayer.<init>(MediaPlayer.java:421)
    at com.aldora.tankwar.Tools.playSound(Tools.java:18)
    at com.aldora.tankwar.App.main(App.java:146)
Caused by: com.sun.media.jfxmedia.MediaException: Could not create player!
    at com.sun.media.jfxmediaimpl.NativeMediaManager.getPlayer(NativeMediaManager.java:295)
    at com.sun.media.jfxmedia.MediaManager.getPlayer(MediaManager.java:118)
    at javafx.scene.media.MediaPlayer.init(MediaPlayer.java:474)
  1. debug result: com.sun.media.jfxmediaimpl.platform.gstreamer.GSTPlatform.createMediaPlayer throws media error: ERROR_MEDIA_NULL调试结果: com.sun.media.jfxmediaimpl.platform.gstreamer.GSTPlatform.createMediaPlayer抛出媒体错误:ERROR_MEDIA_NULL

After some trial and error, I found this can't be walked around by upgrading javafx, switching from javafx11 to javafx16 can solve it.经过反复试验,我发现升级javafx无法解决这个问题,从javafx11切换到javafx16可以解决它。 javafx download link javafx下载链接

The problem is in java's "sound.properties" config file.问题出在 java 的“sound.properties”配置文件中。 I'm now on Ubuntu 20.04, with java-11-openjdk, so, edited /etc/java-11-openjdk/sound.properties and added the following lines at the end:我现在在 Ubuntu 20.04 上,使用 java-11-openjdk,因此,编辑/etc/java-11-openjdk/sound.properties并在末尾添加以下行:

javax.sound.sampled.Clip=com.sun.media.sound.DirectAudioDeviceProvider
javax.sound.sampled.Port=com.sun.media.sound.PortMixerProvider
javax.sound.sampled.SourceDataLine=com.sun.media.sound.DirectAudioDeviceProvider
javax.sound.sampled.TargetDataLine=com.sun.media.sound.DirectAudioDeviceProvider

For deep in, read: https://keithp.com/blogs/Java-Sound-on-Linux/如需深入了解,请阅读: https://keithp.com/blogs/Java-Sound-on-Linux/

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

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