Java 在另一个上播放相同的音频

Java playing the same audio over the another one

我正在制作一个菜单中有鼠标悬停声音的游戏。然而,第二次播放声音时,第一个声音还没有结束,第二个声音被第一个切掉。有什么办法可以解决这个问题吗?

public static AudioClip menuMouseOver; //creating the audioClip
menuMouseOver = Applet.newAudioClip(new File(soundsLocation+"\menuMouseOver.wav").toURL()); //loading the sound into the audioClip
menuMouseOver.play(); //calling the audioClip to play.

AudioClipClipSourceDataLine 未设置同时播放。可以从一个共同的 .wav 文件创建多个音频剪辑或剪辑,并且 Java 的音频系统会将它们混合在一起。但是,在这种情况下,您必须在内存中保存多个音频副本,而且音频非常庞大。

但是对单个音频文件的并发播放进行编码并不是特别棘手。一个相对 straight-forward 的计划是在内存中加载文件的单个副本,然后创建和管理多个 "cursors" 索引到该内存区域以供您播放。每个 "cursor" 的音频数据可以加在一起并通过 SourceDataLine.

输出

这是我用的方案AudioCue, which is meant to be a sort of super Clip, allowing real time volume, panning and pitch changes as well as concurrent playback. I believe TinySound是另一个支持并发播放的音频库。