I'm making a 2D game for Android using Android Studio with Libgdx in Java. I have a class, called SoundPlayer, which holds an ArrayMap. The key is the name of the sound file and the value is the sound object itself which holds all the playing sound instances of the same sound. I noticed a problem: Every time a "hit" sound of a bullet is played repeatedly with small intervals (let's say a machine gun is shooting an enemy and hits it), all sounds stop. With bigger intervals it works fine. This is not a big problem for short sounds, but for looping sounds it is.
Any ideas what causes it to stop?
SoundPlayer class: (playSound is responsible of course of playing sounds).
public class SoundPlayer {
private ArrayMap<String, Sound> sounds = new ArrayMap<String, Sound>();
private Music music;
private Random randomizer = new Random();
public long playSound(String fileName) {
return playSound(fileName, false);
}
public long playSound(String fileName, boolean loop) {
return playSound(fileName,loop, 1);
}
public long playSound(String fileName, boolean loop, float volume) {
if (GameSettings.SOUND_TOGGLE) {
Sound sound = loadSound(fileName);
if (GameSettings.DebugOptions.SOUND_RANDOM_PITCH) {
return setRandomPitch(loop, sound, volume);
}
if (loop) {
return sound.loop(volume);
} else {
return sound.play(volume);
}
}
return -1;
}
private long setRandomPitch(boolean loop, Sound sound, float volume) {
float randomPitch = randomizer.nextFloat() + 0.8f;
if (randomPitch < 0.7 || randomPitch > 1.3) {
randomPitch = 1;
}
long id;
if (loop) {
id = sound.loop(volume, randomPitch, 0);
} else {
id = sound.play(volume, randomPitch, 0);
}
return id;
}
private Sound loadSound(String fileName) {
String soundPath = Rules.System.SFX.SFX_FOLDER_NAME + "/" + Rules.System.SFX.SOUNDS_FOLDER_NAME + "/" + fileName + "." + Rules.System.SFX.SOUNDS_DATA_EXTENSION;
Sound sound;
if (sounds.containsKey(fileName)) {
sound = sounds.get(fileName);
} else {
sound = Main.assetsManager.get(soundPath, Sound.class);
sounds.put(fileName, sound);
}
return sound;
}
public void playMusic(String fileName) {
if (GameSettings.SOUND_TOGGLE && GameSettings.DebugOptions.PLAY_MUSIC) {
music = Main.assetsManager.get(Rules.System.SFX.SFX_FOLDER_NAME + "/" + Rules.System.SFX.MUSIC_FOLDER_NAME + "/" + fileName + "." + Rules.System.SFX.MUSIC_DATA_EXTENSION);
music.setVolume(Rules.System.SFX.MUSIC_VOLUME);
music.play();
music.setLooping(true);
}
}
public void stopMusic() {
if (music != null) {
music.stop();
}
}
public void stopAllSounds() {
if (GameSettings.SOUND_TOGGLE) {
for (int i = 0; i < sounds.size; i++) {
sounds.getValueAt(i).stop();
}
}
}
public void dispose() {
if (GameSettings.SOUND_TOGGLE) {
for (int i = 0; i < sounds.size; i++) {
sounds.getValueAt(i).dispose();
}
}
if (music != null) {
music.dispose();
}
}
public Sound getSoundObject(String fileName) {
return sounds.get(fileName);
}
}
GameObject's play sounds call:
protected long playSound(String sound, boolean loop, float volume) {
if (sound != null) {
return WarScreen.soundPlayer.playSound(sound, loop, volume);
}
return -1;
}
Some output which I didn't find helped much:
04-25 22:37:46.510 15719-15888/com.gadarts.parashoot V/AudioSystem: getFrameCount() streamType 3, output 2, frameCount 960
04-25 22:37:46.510 15719-15888/com.gadarts.parashoot V/AudioSystem: getOutputSamplingRate() reading from output desc
04-25 22:37:46.510 15719-15888/com.gadarts.parashoot V/AudioSystem: getSamplingRate() streamType 3, output 2, sampling rate 48000
04-25 22:37:46.510 15719-15888/com.gadarts.parashoot V/AudioTrack: sampleRate 13705, channelMask 0x1, format 1
04-25 22:37:46.510 15719-15888/com.gadarts.parashoot V/AudioTrack: streamType 3
04-25 22:37:46.510 15719-15888/com.gadarts.parashoot V/AudioTrack: sharedBuffer: 0x7e233000, size: 22050
04-25 22:37:46.510 15719-15888/com.gadarts.parashoot V/AudioTrack: set() streamType 3, sampleRate 13705, format 1, frameCount 0, flags 0004
04-25 22:37:46.520 15719-15888/com.gadarts.parashoot V/AudioSystem: getLatency() streamType 3, output 3, latency 50
04-25 22:37:46.520 15719-15888/com.gadarts.parashoot V/AudioSystem: getFrameCount() streamType 3, output 3, frameCount 960
04-25 22:37:46.520 15719-15888/com.gadarts.parashoot V/AudioSystem: getOutputSamplingRate() reading from output desc
04-25 22:37:46.520 15719-15888/com.gadarts.parashoot V/AudioSystem: getSamplingRate() streamType 3, output 3, sampling rate 48000
04-25 22:37:46.520 15719-15888/com.gadarts.parashoot V/AudioTrack: createTrack_l() output 3 afLatency 50
04-25 22:37:46.530 15719-15888/com.gadarts.parashoot V/AudioTrack: AUDIO_OUTPUT_FLAG_FAST denied by server; frameCount 11025
04-25 22:37:46.540 15719-15888/com.gadarts.parashoot V/SoundNormalizerUtil: lge.normalizer.param = version2.0/true/9.6/true/10500/0.8/5200/0.42
04-25 22:37:46.540 15719-15888/com.gadarts.parashoot V/SoundNormalizerUtil: SoundNormalizer:version = version2.0
04-25 22:37:46.540 15719-15888/com.gadarts.parashoot V/SoundNormalizerUtil: SoundNormalizer:Enable = "true"
04-25 22:37:46.540 15719-15888/com.gadarts.parashoot V/SoundNormalizerUtil: normalizer V2: SoundNormalizer:Enable set to true
04-25 22:37:46.540 15719-15888/com.gadarts.parashoot V/SoundNormalizerUtil: normalizer V2: normalizerMakeupGain = 9.6
04-25 22:37:46.540 15719-15888/com.gadarts.parashoot V/SoundNormalizerUtil: SoundNormalizer:PreFilter_HPF_350Hz = "true"
04-25 22:37:46.540 15719-15888/com.gadarts.parashoot V/SoundNormalizerUtil: normalizer V2: AudioEffect:SoundNormalizer:PreFilter_HPF_350Hz set to true
04-25 22:37:46.540 15719-15888/com.gadarts.parashoot V/SoundNormalizerUtil: normalizer V2: normalizerLimiterThreshold = 10500
04-25 22:37:46.540 15719-15888/com.gadarts.parashoot V/SoundNormalizerUtil: normalizer V2: normalizerLimiterSlope = 0.80
04-25 22:37:46.540 15719-15888/com.gadarts.parashoot V/SoundNormalizerUtil: normalizer V2: normalizerCompressorThreshold = 5200
04-25 22:37:46.540 15719-15888/com.gadarts.parashoot V/SoundNormalizerUtil: normalizer V2: normalizerCompressorSlope = 0.42