gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 19:58:59 25 4
gpt4 key购买 nike

如果我有一个 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com