在Java游戏中播放小声音

发布于 2024-07-08 05:03:34 字数 381 浏览 7 评论 0 原文

对于我正在制作的电脑游戏,我显然想要播放声音。 到目前为止,我一直在使用 AudioClip 来播放 WAV 文件。 虽然这种方法效果很好,但 WAV 文件往往很大。 几秒钟的声音最终会达到数百 kB。 我下载的游戏有 95% 是音频!

这里显而易见的选择是使用 MP3 或 Ogg Vorbis。 但我在这方面取得的成功有限 - 我可以使用 JLayer 播放 MP3(但它在同一线程中播放)。 至于奥格,我一点运气都没有。 更糟糕的是,JLayer 的法律地位有点可疑。

所以我的问题是针对 Java 开发人员和真正了解声音的一般人:我该怎么办? 我能以某种方式“减少”我的 WAV 中的脂肪吗? 有没有什么方法可以用Java玩Ogg? 我应该使用其他声音格式吗?

For the computer game I'm making, I obviously want to play sound. So far, I've been using AudioClip to play WAV files. While this approach works fine, the WAV files tend to be gigantic. A few seconds of sound end up being hundreds of kB. I'm faced with having a game download that's 95% audio!

The obvious option here would be to use MP3 or Ogg Vorbis. But I've had limited success with this - I can play MP3 using JLayer (but it plays in the same thread). As for Ogg, I've had no luck at all. Worse, JLayer's legal status is a bit on the dubious side.

So my question is to both Java developers and generally people who actually know something about sound: What do I do? Can I somehow "trim the fat" off my WAVs? Is there some way of playing Ogg in Java? Is there some other sound format I should use instead?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

只是在用心讲痛 2024-07-15 05:03:34

您可以使用 JOrbis 库来播放 OGG 音乐。 对于工作示例用法,您可以查看这些文件 此处

我还尝试了一些无损音频压缩,例如我有 16 位单声道声音。 我将每个样本的高字节和低字节分开,并将它们放在一起。 然后我应用了差异替换,其中每个字节都被替换为与最后一个字节的差异。 最后使用GZIP来压缩数据。 我能够将数据大小减少到原始大小的 50-60%。 不幸的是这还不够,所以我转向 OGG 格式。

我注意到 8 位音乐的一件事是,如果我改变音量,播放会变得非常嘈杂。 我通过在 SourceDataLine.write() 之前将音频数据上采样到 16 位解决了这个问题。

You could use JOrbis library to play back OGG music. For working sample usage you can look at these files here.

I also experimented with some lossless audio compression, for example I had a 16 bit mono sound. I separated the upper and lower bytes of each sample and put them after each other. Then I applied a differential replacement where each byte is replaced by its difference from the last byte. And finally used GZIP to compress the data. I was able to reduce the data size to 50-60% of the original size. Unfortunately this was not enough so I turned to the OGG format.

One thing I noticed with 8 bit music is that if I change the audio volume, the playback becomes very noisy. I solved this problem by upsampling the audio data to 16 bit right before the SourceDataLine.write().

£冰雨忧蓝° 2024-07-15 05:03:34

这些可能已经过时,但它们得到 Xiph.org 团队(维护 Ogg 和 Vorbis 等)的正式认可。
http://www.vorbis.com/software/#java

These may be outdated, but they are officially recognized by the Xiph.org team (who maintain Ogg and Vorbis, among others).
http://www.vorbis.com/software/#java

绝不服输 2024-07-15 05:03:34

您描述的问题已通过 Java 中声音的服务提供者接口 (SPI) 解决。 结果是,只需将 JAR 文件添加到类路径即可向默认 Java Sound API 添加功能。 从而无需更改代码即可处理更多声音格式。

上次我尝试这个时,Javazoom 人们提供了一个可用的 MP3 SPI JAR。 这是基于你提到的 JLayer 的。

对于 Vorbis OGG,现在似乎还有一个 SPI 库。 查看 Javazoom 上 Vorbis SPI 上的文档。

The problem you describe is addressed by the Service Provider Interface (SPI) for sound in Java. The result is that simply adding JAR files to your classpath will add functionality to the default Java Sound API. Thus enabling the handling of more sound formats without changing code.

Last time I tried this the Javazoom people offered a working MP3 SPI JAR. Which was based on the JLayer you mentioned.

For Vorbis OGG there now also seems to be an SPI library. Check out the docs on the Vorbis SPI on Javazoom.

笑叹一世浮沉 2024-07-15 05:03:34

如果您决定继续使用 wav 格式...

拥有高质量的声音文件可能不是很重要。 您可以使用您最喜欢的 wav 编辑器来降低比特率、切换到单声道,或两者兼而有之。 这将节省大量空间,并且您在玩游戏时不会注意到质量差异。

希望这可以帮助。

If you decide to stay with wav format...

It is probably not very important to have high quality sound files. You can use your favorite wav editor to lower the bit rate, switch to mono, or both. That will save tons of space and you won't notice the difference in quality while playing the game.

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文