java - 在 Java 中,如何同时多次播放同一个音频片段?

标签 java audio

如果我有一个 java.applet.AudioClip 对象,我可以调用它播放一次。但是,如果我多次调用播放,则剪辑将继续重新启动。解决这个问题的唯一方法似乎是加载同一声音文件的多个实例。这似乎很浪费内存。在游戏中,如果发生 20 次爆炸,那么我必须加载 20 个声音文件实例。

我快速查看了 javax.sound 以查看使用它而不是 AudioClip 在这里是否有帮助。我没有看到任何迹象表明它支持同时多次播放同一剪辑。

是否可以使用简单的 java.applet.AudioClip 或 javax.sound 来避免多次加载相同的声音文件?

最佳答案

点这里 http://forums.sun.com/thread.jspa?threadID=5370182

if you're using a clip, you'll have to maintain one copy per object that needs to play the sound concurrently. You could keep one master copy, and then create copies of the clip as necessary to play them, and then just dump the copies to decrease memory consumption.

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.


read the raw sample data into a byte array and whenever a sound needs to be played I read the data from the array with a ByteArrayInputStream which is fed into an AudioInputStream. After the playback has finished I just close the Clip and no system resources are wasted.

关于java - 在 Java 中,如何同时多次播放同一个音频片段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1854616/

相关文章:

java - Struts 2修改JSON变量名

java - Java 中的合成器和音序器(javax.sound.midi) 是什么?

ios - 在 iOS 应用程序中打包 6000 多个声音文件的最佳方法是什么

java - Continuum 作为 Jenkins 的替代品?

java - 我可以使用 Java 8 运行时通过 App Engine Standard 在运行时设置环境变量吗

java - 将 JSON 分配给 Java String 时如何转义特殊字符

python - 如何用静音填充多个音频 wav 文件

audio - 使用pocketsphinx将音频转换为文本时是否有时间(以分钟和秒为单位)

javascript - 在 Lollipop chrome 中通过 JS 播放本地 mp3 文件

java - 有没有一种方法可以使用 for 循环对具有与其关联的字符串的整数求和?