I am encountering problems with playing looping sounds using SoundPool and .OGG files. I have this HashMap set up for finding a sound associated to a name and playing it/stopping it
public void playLoopSound(String soundName){
currentSound = (Integer) soundMap.get(soundName);
if(currentSound != -1){
try{
Logger.log("Playing Loop Sound: " + currentSound);
loopingSound = soundPool.play(currentSound, 1, 1, 0, -1, 1);
} catch (Exception e) {
Logger.log("Sound Playing Error: " + e.getMessage());
}
} else {
Logger.log("Sound Not Found");
}
}
public void stopLoopSound(){
soundPool.stop(loopingSound);
loopingSound = 0;
}
This set up works fine, i start the loop when the character starts walking and stop it when it stops walking.
The sound would however stop playing randomly, usually a minute or so after having been used (being turned on and off)…
Has anyone else encountered similar problems with SoundPool and looped sounds?
Reading the documentation on Soundpool clears up a lot:
-playing a sound you pass an int referring to the loaded sound. This method (and others starting a sound playing), soundpool.play(int) returns an int referring to the threadID in which the sound now plays.
-when you want to stop the sound (or the looping sound) you have to use the int of the threadID you just got back when you started playing the sound, NOT the int of the sound itself!
Within the soundpool class then, you set a private int threadIDInt = mSoundPool.play/setloop/whatever(int soundtobeplayed, int/float volumeleft, int/float volumeright, float speed, int loopornot); [note: the arguments are a bit made up; look ’em up]
To then stop the sound (or pause, or set it to loop or not) you pass mSoundPool.stop(threadIDINT);
TLDR: there’s a differnce between the int which denotes your sound and the internal int soundpool uses to denote the stream in which your current sound is playing.