SpunkTrumpet
SpunkTrumpet

Reputation: 131

Changing from mediaplayer to soundpool

I've got some code setup that plays 3 sound files in my /raw/ folder one after the other, using the android.media.MediaPlayer object. Two of the 3 sounds are selected at random each time the MediaPlayer is called to play.

However, I've realised this really isn't the best way to do so because the sound clips are so short. I've done a bit of looking at android.media.SoundPool. However I'm not really sure how to incorporate SoundPool instead of using the MediaPlayer. Here's what I've got at the moment.

Any pointers on how I can basically change from using a MediaPlayer to SoundPool would be appreciated.

mpButtonOne = MediaPlayer.create(camera_page.this, R.raw.default);

if (mpButtonOne==null){
    //display a Toast message here
    return;
}

mpButtonOne.start();
mpButtonOne.setOnCompletionListener(new OnCompletionListener() {
    public void onCompletion(MediaPlayer mp) {
        int audio = mfile[rnd.nextInt(SOUND_CLIPS)];
        audioReplayPrimary = audio;
        mpButtonOne.reset();
        mpButtonOne = MediaPlayer.create(camera_page.this, audio);

        if (mpButtonOne==null){
            //display a Toast message here
            return;
        }

        mpButtonOne.start();

        mpButtonOne.setOnCompletionListener(new OnCompletionListener() {
            public void onCompletion(MediaPlayer mp) {
                int audio2 = mfile2[rnd.nextInt(SOUND_CLIPS2)];
                audioReplaySecondary = audio2;
                mpButtonOne.reset();
                mpButtonOne = MediaPlayer.create(camera_page.this, audio2);

                if (mpButtonOne==null){
                    //display a Toast message here
                    return;
                }
                mpButtonOne.start();

                mpButtonOne.setOnCompletionListener(new OnCompletionListener() {

                    @Override
                    public void onCompletion(MediaPlayer mp) {
                        mpButtonOne.release();
                    }

UPDATE

    private static SoundPool mSoundPool;
private static AudioManager mAudioManager;
private final int SOUND_CLIPS3 = 5;
private int mfile3[] = new int[SOUND_CLIPS3];
private Random rnd = new Random();

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.sptest);

    mfile3[0] = R.raw.one;
    mfile3[1] = R.raw.two;
    mfile3[2] = R.raw.three;
    mfile3[3] = R.raw.four;
    mfile3[4] = R.raw.five;

    mSoundPool = new SoundPool(3, AudioManager.STREAM_MUSIC, 0);
    mAudioManager = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);


        Button spTest = (Button) findViewById(R.id.buttonSp);
    spTest.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            int audioPre = mfile3[rnd.nextInt(SOUND_CLIPS3)];
            int sound1 = mSoundPool.load(sptestclass.this, audioPre, 1);
            mSoundPool.play(sound1, 1f, 1f, 1, 0, 1f);

    }
    });

Upvotes: 0

Views: 426

Answers (1)

MiStr
MiStr

Reputation: 1223

You can use the android.media.SoundPool object like this:

import android.media.SoundPool;
//...

//inside your class:
//define: mContext = <your context>
private static SoundPool mSoundPool;
private static AudioManager mAudioManager;

//inside your method:
mSoundPool = new SoundPool(3, AudioManager.STREAM_MUSIC, 0);
mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);

int sound1 = mSoundPool.load(mContext, R.raw.sound1, 1); //<context, soundId, priority>
int sound2 = mSoundPool.load(mContext, R.raw.sound2, 1); //<context, soundId, priority>
int sound3 = mSoundPool.load(mContext, R.raw.sound3, 1); //<context, soundId, priority>

//play a sound:
mSoundPool.play(sound1, 1f, 1f, 1, 0, 1f);//soundId (from load()), volLeft, volRight, priority, loop(boolean), playbackRate


//register a callback for onLoad...:
mSoundPool1.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
    @Override
    public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
        //call .play() for the next sound
    }
});


//if you want to explicitly stop the sound:
mSoundPool.stop(sound1);

Upvotes: 1

Related Questions