尝试使用 OpenJFX 播放 .wav 文件时出现间歇性错误

Intermittent error while trying to play .wav file with OpenJFX

我需要一个内部 .wav(在源文件夹中)才能无误地加载到媒体播放器中。

我昨天让它工作并提交了它工作的代码。今天,回滚到该提交显示以下错误:

Exception in Application start method
java.lang.reflect.InvocationTargetException
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:363)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1051)
Caused by: java.lang.RuntimeException: Exception in Application start method
Caused by: java.lang.RuntimeException: Exception in Application start method

    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:900)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication(LauncherImpl.java:195)
    at java.base/java.lang.Thread.run(Thread.java:832)
Caused by: MediaException: UNKNOWN : com.sun.media.jfxmedia.MediaException: Could not create player! : com.sun.media.jfxmedia.MediaException: Could not create player!
Caused by: MediaException: UNKNOWN : com.sun.media.jfxmedia.MediaException: Could not create player! : com.sun.media.jfxmedia.MediaException: Could not create player!

    at javafx.media/javafx.scene.media.MediaException.exceptionToMediaException(MediaException.java:146)
    at javafx.media/javafx.scene.media.MediaPlayer.init(MediaPlayer.java:518)
    at javafx.media/javafx.scene.media.MediaPlayer.<init>(MediaPlayer.java:421)
    at sokoban@0.0.1/Engine.createPlayer(Engine.java:188)
    at sokoban@0.0.1/Engine.<init>(Engine.java:37)
    at sokoban@0.0.1/initializeGame(Main.java:104)
    at sokoban@0.0.1/start(Main.java:60)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1(LauncherImpl.java:846)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait(PlatformImpl.java:455)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater(PlatformImpl.java:428)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater(PlatformImpl.java:427)
    at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
    at javafx.graphics/com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
    at javafx.graphics/com.sun.glass.ui.gtk.GtkApplication.lambda$runLoop(GtkApplication.java:277)
    ... 1 more
Caused by: com.sun.media.jfxmedia.MediaException: Could not create player!
Caused by: com.sun.media.jfxmedia.MediaException: Could not create player!

    at javafx.media/com.sun.media.jfxmediaimpl.NativeMediaManager.getPlayer(NativeMediaManager.java:295)
    at javafx.media/com.sun.media.jfxmedia.MediaManager.getPlayer(MediaManager.java:118)
    at javafx.media/javafx.scene.media.MediaPlayer.init(MediaPlayer.java:474)
    ... 14 more
Exception running application Main

我想强调一下,同样的代码昨天运行良好。

我发现 this 问题,但它似乎已过时。不管我安装了 Arch 上可用的最新版本的 ffmpeg。

我也找到了 this stack overflow post,但由于它较早地工作,我相信我正确地引用了该文件。

以防万一,这里是文件结构:

main
- java
  - Engine.java
- resources
  - audio.wav

以及创建播放器的代码:

Media music = new Media(getClass().getResource("/audio.wav").toURI().toString());
player = new MediaPlayer(music);

根据 ArchWiki,该异常可能会由于某些不兼容性而引发。

可行的解决方案是安装 ffmpeg-compat-55 AUR 包。