Search code examples
androidsoundpool

Android SoundPool sound playback intermittent


I have several GridView items that play a small .ogg file when they are selected. They are played using the SoundPool class, however this class seems very intermittent - sometimes the sound plays, sometimes it doesn't; there is no pattern to this so I am having trouble figuring out why it's doing it.

Here is my code:

public void playSelectionSound(){
        SoundPool sp = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
        int soundId = sp.load(this, R.raw.char_select, 1);
        sp.play(soundId, 1, 1, 0, 0, 1);
    }

Solution

  • I eventually resorted to switching to MediaPlayer instead of SoundPool:

    MediaPlayer mp = MediaPlayer.create(this, R.raw.char_select); //open media player
            mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                    mp.release();
                }
            });
            mp.start(); //play the sound
    

    Works everytime.