After several days of researching on the Internet, I came here to find help. I'm currently developing a short 2D game for friends (and really just for fun), and I found out about the clips a few days ago. In this game, the player can collect items (almost like coins in Mario). My problem is that I have a very short sound (~ 1 sec for 50 kB) that played when collecting coins, and if the player collected, let him say 3 coins in 1 second, then the clip will lag in the game. If the use case clip is over, then there is no lag. But if the precedent of the Clip has not ended, then an attempt to play the Clip will again lag in the game (a lot).
I have a very small computer, not very powerful, but this problem is really annoying. I get the same sound issue when a player drops a weapon. I have a short clip, and if a player throws this weapon too fast, the backlog in the game ...
Here is what I have already tried:
- Use an array of clips (the same sound) and play back the clip that is not currently playing.
- Use different clips (of the same sound) and the same as before
- Make a few copies (of the same sound) and load them into different clips
- Create a separate thread to play sounds, but I don't like the thread: /
But not one of them changes this problem ...
Now here is part of my code. First up is the class that I use to load sound into a clip.
package sound;
import java.io.IOException;
import java.net.URL;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import utils.Utils;
public class SoundLoader {
public static Clip loadSound(String path) {
Utils.log("Loading " + path + " ... ");
try {
URL url = SoundLoader.class.getResource(path);
AudioInputStream audioIn = AudioSystem.getAudioInputStream(url);
AudioFormat format = audioIn.getFormat();
DataLine.Info info = new DataLine.Info(Clip.class, format);
Clip clip = (Clip)AudioSystem.getLine(info);
clip.open(audioIn);
Utils.log("success\n");
return clip;
} catch (UnsupportedAudioFileException | IOException | LineUnavailableException e) {
e.printStackTrace();
Utils.log("failed !\n");
return null;
}
}
}
Now the class in which I will control all the sounds:
package sound;
import javax.sound.sampled.Clip;
import principal.Handler;
public class SoundManager {
private Clip woosh;
private Clip coin1;
private Clip coin2;
public SoundManager(Handler handler) {
woosh = SoundLoader.loadSound("/resources/sounds/woosh2.wav");
coin1 = SoundLoader.loadSound("/resources/sounds/coin1.wav");
coin2 = SoundLoader.loadSound("/resources/sounds/coin2.wav");
}
public void wooshClip() {
startClip(woosh);
}
public void coin1Clip() {
startClip(coin1);
}
public void coin2Clip() {
startClip(coin2);
}
public synchronized void startClip(Clip clip) {
clip.stop();
clip.setFramePosition(0);
clip.start();
}
public void loopClip(Clip clip) {
clip.loop(Clip.LOOP_CONTINUOUSLY);
}
public void stopClip(Clip clip) {
clip.stop();
}
}
, , Clip, startClip() "Player" ( ).
handler.getSoundManager().wooshClip();
, Clip , starClip() . ? , ? , ?
, ... , . - , , !
, !
EDIT: , - ... ! 50 ? ...