Question

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);
}
Was it helpful?

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.

OTHER TIPS

I do not know the cause of that crackle, but I have two suggestions:

  1. set the maxStreams bigger than 1, re-increment the priority and see if it has some effects.

  2. set setOnLoadCompleteListener before loading your sound file:

    sp.setOnLoadCompleteListener(new OnLoadCompleteListener() {
        @Override
        public void onLoadComplete(SoundPool soundpool, int sampleId, int status) {
            loaded = true;
        }
    });
    

    and then check the flag before you play the file,

    if (loaded)
        sndPlaying = sp.play(sndBeep, 1.0f, 1.0f, sndPriority, 0, sndFreq);
    
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top