title: how to handle media player efficiently?
body: <p>This code works so far on devices I have tested, (ALL mdpi level devices).  </p>
  
  <pre><code>private void stopSounds() {
      if (mp!=null &amp;&amp; mp.isPlaying()) {
          mp.stop();
          mp.reset();
          mp.release();
          mp = null;
      }
  }
  private void playSounds(int sound) {
      stopSounds();
      mp = new MediaPlayer();
      mp = MediaPlayer.create(this, sound);
      mp.start();
      }
  @Override
  public void onClick(View v) {
      switch (v.getId()) {
      case R.id.button1:
          playSounds(R.raw.correct_amazing);
          break;
      case R.id.button2:
          playSounds(R.raw.correct_awesome);
          break;
      case R.id.button3:
          playSounds(R.raw.correct_chick);
          break;
      default:
          break;
      }
  }
  </code></pre>
  
  <p>No errors or exceptions so far, I considered releasing the MediaPlayer object(mp) after it has completed (playing).</p>
  
  <p>So I just tried adding onCompletionListener as follows, in playSounds() : </p>
  
  <pre><code>    private void playSounds(int sound) {
      stopSounds();
      mp = new MediaPlayer();
      mp = MediaPlayer.create(this, sound);
      mp.start();
  
      mp.setOnCompletionListener(new OnCompletionListener() {
          @Override
          public void onCompletion(MediaPlayer mep) {
              mep.stop();
              mep.reset();
              mep.release();
              mep = null;
          }
      });
  }
  </code></pre>
  
  <p>Then it started throwing "IllegalStateException" as follows, </p>
  
  <pre><code>  01-22 17:51:24.150: W/dalvikvm(3081): threadid=1: thread exiting with uncaught exception (group=0x41e2e498)
    01-22 17:51:24.150: E/test(3081): Exception
    01-22 17:51:24.180: E/AndroidRuntime(3081): FATAL EXCEPTION: main
    01-22 17:51:24.180: E/AndroidRuntime(3081): java.lang.IllegalStateException
    01-22 17:51:24.180: E/AndroidRuntime(3081):   at android.media.MediaPlayer.isPlaying(Native Method)
    01-22 17:51:24.180: E/AndroidRuntime(3081):   at com.Test.mediaplayertert.MPActivity.stopSounds(MPActivity.java:30)
    01-22 17:51:24.180: E/AndroidRuntime(3081):   at com.Test.mediaplayertert.MPActivity.playSounds(MPActivity.java:39)
    01-22 17:51:24.180: E/AndroidRuntime(3081):   at com.Test.mediaplayertert.MPActivity.onClick(MPActivity.java:61)
    01-22 17:51:24.180: E/AndroidRuntime(3081):   at android.view.View.performClick(View.java:4106)
    01-22 17:51:24.180: E/AndroidRuntime(3081):   at android.view.View$PerformClick.run(View.java:17150)
    01-22 17:51:24.180: E/AndroidRuntime(3081):   at android.os.Handler.handleCallback(Handler.java:615)
    01-22 17:51:24.180: E/AndroidRuntime(3081):   at android.os.Handler.dispatchMessage(Handler.java:92)
    01-22 17:51:24.180: E/AndroidRuntime(3081):   at android.os.Looper.loop(Looper.java:137)
    01-22 17:51:24.180: E/AndroidRuntime(3081):   at android.app.ActivityThread.main(ActivityThread.java:4793)
    01-22 17:51:24.180: E/AndroidRuntime(3081):   at java.lang.reflect.Method.invokeNative(Native Method)
    01-22 17:51:24.180: E/AndroidRuntime(3081):   at java.lang.reflect.Method.invoke(Method.java:511)
    01-22 17:51:24.180: E/AndroidRuntime(3081):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:808)
    01-22 17:51:24.180: E/AndroidRuntime(3081):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:575)
    01-22 17:51:24.180: E/AndroidRuntime(3081):   at dalvik.system.NativeStart.main(Native Method)
  </code></pre>
  
  <p>My problem is I have some 6 buttons on screen and user presses them as long as they are on the activity and I need to play "correct" sounds, "wrong" sounds respectively. So, it will take continuous pressings.</p>
  
  <p>My above error is occuring after mp object is released via setOnCompletionListener().</p>
  
  <p><strong>I want to know how to efficiently handle mediaplayer in the above mentioned conditions (Or) How to Release mediaplayer object after it has completed playing?</strong></p>