0

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.
Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Dark Fox
  • 5
  • 1
  • 3
  • 2
    First, don't use a `JApplet`, they are deprecated and no longer supported, additionally, using `File` in a `JApplet` is a anti-pattern, it makes no sense, as applets are contained in a relatively tight security container, which normally precludes accessing the local file system – MadProgrammer Jun 23 '19 at 22:40
  • You might consider checking out https://stackoverflow.com/questions/29836255/playing-multiple-sound-clips-using-clip-objects/29836764#29836764 for a example of playing multiple audio sounds over each other – MadProgrammer Jun 23 '19 at 22:42

1 Answers1

0

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.

Phil Freihofner
  • 7,645
  • 1
  • 20
  • 41