简体   繁体   English

是否可以使用JavaFX.scene.media.Media从JAR文件中播放MP3或WAV文件?

[英]Is it possible to play an MP3 or WAV file from inside a JAR file using JavaFX.scene.media.Media?

I'm fairly certain the answer is "no", but I want to release this application with default audio files contained in the jar. 我相当确定答案是“否”,但是我想使用jar中包含的默认音频文件发布此应用程序。 If possible I would like to be able to simply stream them as a resource into a Media file and play that media file, something like this: 如果可能的话,我希望能够简单地将它们作为资源流式传输到媒体文件并播放该媒体文件,如下所示:

import javafx.scene.media.Media;
.
.
.
Media m = new Media(this.getClass().resourceAsStream("File.mp3"));
//or
m = new Media(this.getClass().getResource("File.mp3"));

Is this possible? 这可能吗? It keeps telling me "Bla bla URL can cannot be converted to String". 它总是告诉我“ Bla bla URL无法转换为字符串”。 If it's not possible I can live with it (I can just dump the audio files to a temporary directory and use them from there, and then delete them once the program is closed), but I would much prefer to just use them from within the jar. 如果不可能的话,我可以使用它(我可以将音频文件转储到一个临时目录中,然后从那里使用它们,然后在程序关闭后将其删除),但我宁愿仅在罐。

According to the documentation , the constructor for Media can only take a string, not a Stream. 根据文档 ,Media的构造函数只能采用字符串,而不能采用Stream。 The string should be a URL, which may be a JAR URL. 该字符串应为URL,可以为JAR URL。

You should be able to construct a JAR URL to the file in your JAR on the file system... documentation for a JAR URL is at http://docs.oracle.com/javase/6/docs/api/java/net/JarURLConnection.html . 您应该能够在文件系统上的JAR中构造指向该文件的JAR URL ... JAR URL的文档位于http://docs.oracle.com/javase/6/docs/api/java/net /JarURLConnection.html

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

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