Note that there are some explanatory texts on larger screens.

plurals
  1. POChanging from mediaplayer to soundpool
    primarykey
    data
    text
    <p>I've got some code setup that plays 3 sound files in my <code>/raw/</code> folder one after the other, using the <code>android.media.MediaPlayer</code> object. Two of the 3 sounds are selected at random each time the MediaPlayer is called to play.</p> <p>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 <code>android.media.SoundPool</code>. However I'm not really sure how to incorporate <code>SoundPool</code> instead of using the <code>MediaPlayer</code>. Here's what I've got at the moment.</p> <p>Any pointers on how I can basically change from using a MediaPlayer to SoundPool would be appreciated.</p> <pre><code>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(); } </code></pre> <p>UPDATE</p> <pre><code> 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); } }); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload