Search code examples
androidsoundpool

How can I get rid of crackling noise when playing multiple sounds with SoundPool


My app plays a short beep whenever the user touches a tile. Sounds perfect. However, if the user drags their finger it will play the beep multiple times in quick succession, which works but also makes a crackling, static kind of noise. I'm trying to get rid of the crackle.

I've tried many things that have not worked:

  • Increasing the SoundPool max streams to 8 or 16 - no effect
  • incrementing sndPriority so that each call to play() uses a higher priority than the last one - no effect
  • calling sp.stop(sndPlaying) on the previous sound - this actually makes the crackle worse
  • Tested on two different devices with different Android OS versions, same crackle
  • I've tinkered with the sound file in Audacity and I am reasonably confident the crackle is not a problem with the sound file

Code:

public void setup() {
  sp = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
  sndBeep = sp.load(panel.act.getApplicationContext(), R.raw.beep, 1);
}
public void play() {
  // sp.stop(sndPlaying);   -- this actually makes the crackling noise worse
  // sndPriority++;         -- this had no effect
  sndPlaying = sp.play(sndBeep, 1.0f, 1.0f, sndPriority, 0, sndFreq);
}

Solution

  • The noise happens because of the little pause between the last and the next beep. So, in fact you should decrement the priority on each call.

    This should finish the last beep and start the new one only after that.

    There's still a chance to get some noise. Simply if the sound contains - even a tiny - noise at beginning or end.