问题描述
如果我有一个java.applet.AudioClip中的对象,我可以调用它的发挥,一旦玩。但是,如果我打打电话多次,然后剪辑将仅保留重新启动。要解决这个问题的唯一方法似乎是加载同一个声音文件的多个实例。这似乎是浪费内存。在游戏中,如果我有发生爆炸20,那么我必须加载的声音文件的20个实例。
If I have a java.applet.AudioClip object I can call play on it to play it once. But if I call play multiple times then the clip will simply keep restarting. The only way to get around that seems to be to load multiple instances of the same sound file. That seems wasteful of memory. In a game, if I have 20 explosions occurring then I'd have to load 20 instances of the sound file.
我刚刚看了一下在javax.sound,看是否使用它来代替音频剪辑的,将有助于在这里。我没有看到任何表明它支持相同的剪辑多次同时播放。
I took a quick look at javax.sound to see if using it instead of AudioClip would help here. I don't see anything that indicates that it supports playing the same clip multiple times simultaneously.
是否有可能没有任何与简单java.applet.AudioClip中或与javax.sound东西多次加载相同的声音文件?
Is it possible to do without loading the same sound file multiple times either with the simple java.applet.AudioClip or with the javax.sound stuff?
推荐答案
在这里检查
<一href=\"http://forums.sun.com/thread.jspa?threadID=5370182\">http://forums.sun.com/thread.jspa?threadID=5370182
如果您使用的是夹子,你必须要保持每个对象的一个副本,需要同时播放声音。你可以保留一个主副本,然后创建在必要时发挥他们剪辑的副本,然后只转储副本,以减少内存消耗。
第二个想法是写自己的剪辑类。 Esentially,所有你需要做的是转储一些声音数据到TargetDataLine的进行播放。它会处理的缓冲和正确的速度本身播放。如果这是你所做的一切,你应该能够从同一个实例转储多次,并多次播放。
A second idea would be to write your own Clip class. Esentially, all you would need to do is dump some sound data onto a TargetDataline to play it. It'll handle the buffering and playing at the correct speed itself. If that's all you did, you should be able to dump multiple times from the same instance and play it multiple times.
读取的原始样品数据转换成一个字节数组,每当一个声音需要被播放我从与被送入的AudioInputStream一个ByteArrayInputStream阵列读取数据。
播放结束后,我只是关闭剪辑,也没有造成系统资源浪费。
这篇关于在Java中,我怎么能同一个音频剪辑多次同时玩?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!