简体   繁体   中英

Java playing the same audio over the another one

I am making a game which has mouse over sound in the menu. However the 2nd time when the sound is played and the first one didn't finished yet the 2nd cuts the first. Is there any way to fix that issue?

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.

AudioClip , Clip , SourceDataLine are not set up for concurrent playback. It is possible to create multiple AudioClips or Clips from a common .wav file, and Java's audio system will mix these together. In this case, though, you have to hold multiple copies of the audio in memory, and audio is pretty bulky.

But coding concurrent playbacks of a single audio file is not particularly tricky. A relatively straight-forward plan is to load a single copy of the file in memory, and then create and manage multiple "cursors" that index into this memory area for your playback. The audio data from each "cursor" can be added together and output via a SourceDataLine .

This is the plan I used for 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 is another audio library that enables concurrent playback.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM