MediaPlayer 仅在 JAR 文件中抛出 MediaException 在普通 Java 应用程序中使用 JavaFX MediaPlayer 播放音频?
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.
- 本文标签: 家庭宠物
- 本文链接: https://www.coder6.net/article/2383
- 版权声明: 本文由蚂蚁原创发布,转载请遵循《署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)》许可协议授权
热门推荐
-
浏览(193) 评论(0)