基于 Java 的游戏的声音格式
我正在开发一个 Java 游戏,想要捆绑并播放许多采样的音效。我计划使用标准 javax.sound.sampled 功能来播放它们。
什么格式最适合此类样本?我正在寻找良好的压缩、良好的质量和方便的 Java 编程使用的结合体。
I'm developing a Java game and want to bundle and play a number of sampled sound effects. I'm planning to use the standard javax.sound.sampled functionality to play them.
What format is best for these kind of samples? I'm looking for a blend of good compression, decent quality and convenience for programmatic use within Java.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
质量与大小
这里有一些关于 MP3 与 WAV 差异的有趣文章。
http://www.angelfire.com/vt2/tommymc3/MP3vsWav.html
http: //www.scinet.cc/articles/wma-vs-mp3/wma-mp3.html
http://ask.yahoo.com/20000602.html
为什么选择 MP3 和 WAV?这些是我在创建程序时最常看到的,因此兼容性从来不是问题。
我有这个非常有用的 java 文件,它可以为你做一切事情。您可以使用声音文件的路径构造对象,然后使用方法来播放、停止、循环播放等。
同时,您可以查看这些作为参考,尽管它们并不那么干净: 如何在 Java 中播放声音? 。
简单编程
不幸的是,我的计算机上没有该文件,但这里有一个简化版本。这并不像您希望的那样使用 javax.swing,但说实话,我一直更喜欢替代方法。
因为这与我在其他计算机上的文件不同,所以我无法保证 MP3 兼容性。
实例化:
现在,只要您想要播放剪辑,就可以调用
helloSound.play();
。我更喜欢这种方法,这样您就不必在每次想要播放剪辑时不断地将所有内容设置为流。这应该可以通过一些声音片段和简介有效地工作,但请确保不要过度使用它并占用内存。使用它来实现常见的声音效果。
简单编程,续
找到了一个很好的文件,可以按照我上面展示的相同方式播放 MP3。我完全忘记把我的放在一个单独的线程中,所以这是一个更好的选择。
简单易行,对吧? ;)。
在此处获取 jar。请参阅此处的文档。
Quality vs. Size
Here are some interesting articles on the differences of MP3 vs. WAV.
http://www.angelfire.com/vt2/tommymc3/MP3vsWav.html
http://www.scinet.cc/articles/wma-vs-mp3/wma-mp3.html
http://ask.yahoo.com/20000602.html
Why MP3 and WAV? These are the ones I see most often when creating program, so compatibility is never an issue.
I have this really useful java file that does everything for you. You construct the object with the path to the sound file, then use methods to play, stop, loop it, etc.
In the meantime, you can look at these for reference, albeit they're not as clean: How can I play sound in Java? .
Simple Programming
Unfortunately, I don't have the file on this computer, but here's a simplified version. This doesn't use javax.swing like you'd hoped, but to be honest, I've always preferred alternative methods.
Because this is not the same as the file I have on my other computer, I cannot guarantee MP3 compatibility.
Instantiation:
Now you can call
helloSound.play();
whenever you want the clip to be played.I prefer this method so you don't have to constantly set everything up a stream each time you want to play a clip. This should work effectively with a few sound bites and blurbs, but be sure not to overuse it and take up memory. Use this for common sound effects.
Simple Programming, Cont'd
Found a good file to play MP3s in the same way as I showed above. I completely forgot to put mine in a separate thread, so this is a much better bet.
Easy as pie, right? ;).
Get the jar here. See the documentation here.
Java 支持 mp3,而 mp3 格式使您可以选择在压缩与质量之间进行平衡。
有一个纯java MP3解码器,http://www.javazoom.net/javalayer/javalayer。 html,它插入 javax.sound 包中。 Sun 还发布了 mp3 插件 java的声音。
Java has support for mp3, and the mp3 format gives you the choice where to balance compression vs. quality.
There is a pure java MP3 decoder, http://www.javazoom.net/javalayer/javalayer.html, which plugs into the javax.sound package. Sun also have released a mp3 plugin for java sound.
我不是这方面的专家,但 Sun 的所有教程和示例都适用于
.au
文件。似乎是有保证的最低公分母。为了使文件格式更加灵活,您需要使用类似 的内容Java 媒体框架。I'm no expert on this, but all the tutorials and examples from Sun work with
.au
files. Seems to be a guaranteed lowest-common-denominator. For more flexibility in file formats, you'd need to work with something like the Java Media Framework.