简体   繁体   English

如何在Netbeans之外播放.wav或.mid文件?

[英]How do I get a .wav or a .mid file to play outside of netbeans?

I have created a simply login screen for a test game and it plays a .mid music tune. 我为测试游戏创建了一个简单的登录屏幕,它播放.mid音乐调。 The .wav sounds are played when you hover over a button. 将鼠标悬停在按钮上时,将播放.wav声音。 When I Build+Compile the program, the both sounds play inside netbeans. 当我构建+编译程序时,两种声音都在netbeans中播放。 When i run the .jar file outside of netbeans, it does not work. 当我在netbeans之外运行.jar文件时,它不起作用。 Any suggestions... 有什么建议么...

PS The sounds are in a folder, inside the src folder, called resources. PS声音位于src文件夹中的一个文件夹中,称为资源。 For .mid music, I use the sequence, and for .wav I use AudioInputStream and such. 对于.mid音乐,我使用序列,对于.wav,我使用AudioInputStream等。

It is most likely that you are attempting to access application resources as though they were a File . 您很可能试图将应用程序资源当作File来访问。 An application resource would usually be inside a Jar file, and must be accessed by URL . 应用程序资源通常位于Jar文件中,并且必须通过URL进行访问。 To form the URL, use something like: 要形成URL,请使用类似以下内容的内容:

URL urlToMid = this.getClass().getResource("the.mid");

If that is not the case, then the next most likely problem is that the resource is not being included in the Jar. 如果不是这种情况,那么下一个最可能的问题是该资源未包含在Jar中。

Without a code example, it's hard to help you out. 没有代码示例,很难为您提供帮助。 Do you get an exception? 你有例外吗?

My guess is that your application cannot find the music files when running outside of NetBeans. 我的猜测是,在NetBeans之外运行时,您的应用程序找不到音乐文件。 Your home directory must be set to some value while running from NetBeans (probably pointing to your src/resource folder), but you specify a different home location (or none at all) when running outside NetBeans. 从NetBeans运行时,必须将主目录设置为某个值(可能指向src / resource文件夹),但是在NetBeans外部运行时,您可以指定其他主目录(或根本不指定)。

If the files are included in the generated .jar file, another possible problem is case-sensitivity. 如果文件包含在生成的.jar文件,另一个可能的问题是区分大小写。 If the file is load from the regular filesystem (eg from within NetBeans), it depends on your operating system's filesystem if the filename is case-sensitive or not. 如果文件是从常规文件系统(例如从NetBeans内部)中加载的,则文件名是否区分大小写取决于您操作系统的文件系统。 On Windows it is not. 在Windows上不是。

Once the file is loaded through a classloader (eg using getResource()), the filename is case sensitive and Alert.wav is a different file than alert.wav . 一旦文件被通过一个类加载器加载(例如,使用的getResource()),文件名是大小写敏感的Alert.wav是一个不同的文件比alert.wav

Make sure the filename in the source code is exactly the same way as it appears in the filesystem. 确保源代码中的文件名与文件系统中出现的文件名完全相同

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

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