Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><code>SoundPool</code> is a much better alternative for this purpose. I would caution strongly against instantiating multiple <code>MediaPlayer</code> instances as most systems do not have the resources to generate many parallel active instances. You wil find on many device that hitting the button upwards of 5 times will cause a memory based crash.</p> <p>As far as stopping all active streams, there is not baked-in function for this, but it's easy to accomplish in a manner to similar to your existing code. As a side note, there is an <code>autoPause()</code> method, which halts all streams, but it doesn't truly end their playback (as the method name insinuates). Here is a simple example to manage your audio streams:</p> <pre><code>//SoundPool initialization somewhere SoundPool pool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0); //Load your sound effect into the pool int soundId = pool.load(...); //There are several versions of this, pick which fits your sound List&lt;Integer&gt; streams = new ArrayList&lt;Integer&gt;(); Button item1 = (Button)findViewById(R.id.item1); item1.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { int streamId = pool.play(soundId, 1.0f, 1.0f, 1, 0, 1.0f); streams.add(streamId); } }); Button stop = (Button)findViewById(R.id.stop); stop.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { for (Integer stream : streams) { pool.stop(stream); } streams.clear(); } }); </code></pre> <p>It is much more memory efficient to manage a list of streamID values than <code>MediaPlayer</code> instances, and your users will thank you. Also, note that it is safe to call <code>SoundPool.stop()</code> even if the streamID is no longer valid, so you don't need to check for existing playback.</p> <p>HTH</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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