原创

MediaPlayer 仅在 JAR 文件中抛出 MediaException 在普通 Java 应用程序中使用 JavaFX MediaPlayer 播放音频?

温馨提示:
本文最后更新于 2024年04月12日,已超过 48 天没有更新。若文章内的图片失效(无法正常加载),请留言反馈或直接联系我

I build a litte Audio Player in Java 11 and JavaFX for Windows. Right now I have only a demo. After that i wrote it in my IDE (Intellj Idea), I decide to build the JAR file to make sure it was right. Inside the IDE the the demo is perfect, but if I try to use the JAR file it won't open. Only using the Windows CMD i get some error

"C:\Program Files\OpenLogic\jre-11.0.22.7-hotspot\bin\java.exe" -jar  "Demo-jar.jar"
com.sun.media.jfxmedia.MediaException: Could not create player!
Exception in Application start method
Exception in thread "main" java.lang.RuntimeException: Exception in Application start method
        at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(Unknown Source)
        at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(Unknown Source)
        at java.base/java.lang.Thread.run(Unknown Source)
Caused by: javafx.fxml.LoadException:
file:/C:/[path]/demo_jar/demo-jar%202.jar!/com/example/demo/primary.fxml

        at javafx.fxml/javafx.fxml.FXMLLoader.constructLoadException(Unknown Source)
        at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
        at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
        at javafx.fxml/javafx.fxml.FXMLLoader.load(Unknown Source)
        at com.example.demo.App.loadFXML(App.java:31)
        at com.example.demo.App.start(App.java:20)
        at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(Unknown Source)
        at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(Unknown Source)
        at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(Unknown Source)
        at java.base/java.security.AccessController.doPrivileged(Native Method)
        at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(Unknown Source)
        at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
        at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
        at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(Unknown Source)
        ... 1 more
Caused by: java.lang.NullPointerException
        at com.example.demo.Controller.initialize(Controller.java:35)
        ... 14 more

I don't know why in the IDE works, and in the JAR file not.

This is the code that throws Exception

public AudioPlayer(String audioFolderPath) throws Exception {
    File dir = new File(audioFolderPath);
    if (!dir.exists()) {
        if (!dir.mkdir())
            throw new Exception("Impossibile creare la cartella Sound");
    }

    this.songsArray = new ArrayList<>();
    this.songsArray.addAll(Arrays.asList(dir.listFiles()));

    if (!this.songsArray.isEmpty()) {
        this.media = new Media(songsArray.get(0).toURI().toString());
        this.mediaPlayer = new MediaPlayer(media);
    } else {
        this.media = null;
        this.mediaPlayer = null;
    }
}

For the needs of the App, i can't include the Audio file (.mp3) inside the JAR file.

Someone have some ideas? Thanks for helping and sorry for my bad English

All the files exist, they can be read and execute. I've seen another post, still on StackOverflow, about this topic. They said how on Ubutu the way i create the Media is wrong, so i tried also

            this.media = new Media("file:/C:/Sound/Campanello_Casa.mp3");

but nothing change.

正文到此结束
热门推荐
本文目录