如何播放背景音乐程序运行时?在java中 [英] How to play a background music when the program run? in java

查看:658
本文介绍了如何播放背景音乐程序运行时?在java中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想提出一个纸牌游戏,而且它几乎完成了。我想做的最后一件事是播放一些背景音乐。现在,如果我将文件复制到默认的包,使游戏的一个JAR文件,将音乐的所有计算机上玩吗?目前,我的电脑上它没有任何问题,运行给予一个特定的路径,比如C:\\\\ samp.wav文件; 。但我担心,如果我做一个jar文件,另一台PC上运行它,它会无法正常工作。我认为会有一个 FileNotFoundException异常。我是对还是错?

I am making a card game, and it's almost done. The last thing I want to do is play some background music. Now if I copy the file into the default package and make a single jar file of the game, will the music play on all computers? Currently, on my PC it's running without any problem by giving a specific path for the file like "C:\\samp.wav";. But I am worried that if I make a jar file and run it on another PC it won't work properly. I think there will be a FileNotFoundException. Am I right or wrong?

有关卡的形象我使用这行:

For the card's image I am using this line:

jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/1.jpg")));

我已经插进我的默认包那些照片。我想要做的音乐文件是相同的,但如何?我使用NetBeans。

Those pictures I have inserted into my default package. I want to do the same for the music file, but how? I am using NetBeans.

推荐答案

您应该包含你的应用罐内的wav文件。这样,您就不必来管理用户的文件系统中的文件的复印件(请记住,例如,在UNIX,MAC等则无法访问硬盘驱动器通过 C:/ ...

You should include the wav file inside your application jar. This way you won't have to manage the copy of the file in the user's file system (keep in mind that, for example, in UNIX, Mac, etc. you can't access to the hard drive through C:/...).

例如,如果您将wav文件的应用程序JAR的根目录( app.jar / samp.wav

For example, if you place the wav file in the root of the app jar (app.jar/samp.wav):

InputStream is = getClass().getClassLoader().getResourceAsStream("samp.wav");

或者,如果你有在应用程序的jar根声音目录( app.jar /声音/ samp.wav

InputStream is = getClass().getClassLoader().getResourceAsStream("sounds/samp.wav");

帖子额外的信息有关播放的wav文件, Java的(虽然你的问题,我想你已经解决了这个问题)。考虑也发挥它在一个单独的线程。检查还本网站的范例有关使用Java管理媒体文件。

Check this post for extra information about playing wav files with Java (though for your question, I think you have already solved this problem). Consider as well playing it in a separate thread. Check also this web for examples about managing media files in Java.

这篇关于如何播放背景音乐程序运行时?在java中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆