title: Android MediaPlayer - setDataSource and Release - IllegalStateException
body: <p>I wrote my own MediaPlayer class to play files at a specific path and to play files from the assets folder. Here is the class:</p>
  
  <pre><code>public class CMediaPlayer extends MediaPlayer{
  
  public void play(String audioPath){
  
     this.setOnCompletionListener(new OnCompletionListener() {
          @Override
          public void onCompletion(MediaPlayer mp) {
              mp.release();
          }
      });
  
      File f = new File(audioPath);
      if(f.exists()){
          try{
              FileInputStream fis = new FileInputStream(f);
              FileDescriptor fileD = fis.getFD();
              this.setDataSource(fileD);
              this.prepare();
  
          }catch(IOException e){
  
          }
         this.start();
  }
  }
  
      public void play(AssetFileDescriptor descriptor){
  
          this.setOnCompletionListener(new OnCompletionListener() {
              @Override
              public void onCompletion(MediaPlayer mp) {
                 mp.release();
              }
          });
          try {
              this.setDataSource(descriptor.getFileDescriptor(), descriptor.getStartOffset(), descriptor.getLength());
              descriptor.close();
              this.prepare();
          }catch (IOException e){
  
          }
          this.start();
      }
  
  }
  </code></pre>
  
  <p>I want to play several sounds with that class from an activity. Here is my code:</p>
  
  <pre><code>public class playGame extends Activity {
  
  //a lot of variables
  
      CMediaPlayer mediaPlayer; //declare my mediaplayer
  
      @Override
      public boolean onKeyDown(int keyCode, KeyEvent event) {
          if (keyCode == KeyEvent.KEYCODE_BACK ) {
              //release??????
              Intent myIntent = new Intent(getBaseContext(), startView.class);
              startActivity(myIntent);
          }
          return super.onKeyDown(keyCode, event);
      }
  
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_play_question2);
  
          mediaPlayer = new CMediaPlayer(); //define my mediaplayer
  
         //stuff
      }
  
      //more variables
  
      public void playQuestion(File question){
  
          //stuff
  
          TextView myTextView = (TextView) findViewById(R.id.textViewQuestion);
          //stuff
          myTextView.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
              mediaPlayer.play(pathSoundQuestion); //play sound when clicked
              }
          });
  
      //stuff
  
          myImageView.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                 mediaPlayer.play(pathSoundQuestion); //play sound when clicked
              }
          });
  
          //stuff
          mediaPlayer.play(pathSoundQuestion); //plays sound immediatly, first played sound (works fine)
  
          //button1
          Button myButton = (Button) findViewById(R.id.button1);
          //stuff
          myButton.setOnClickListener(new Button.OnClickListener() {
              @Override
              public void onClick(View v) {
                  if(lastClickedButton == v){
                     //stuff
                      return;
                  }
                  //stuff
                  mediaPlayer.play(pathAudio1); //play sound when clicked (error)
              }
          });
  
          //button2
          myButton = (Button) findViewById(R.id.button2);
         //stuff
          myButton.setOnClickListener(new Button.OnClickListener() {
              @Override
              public void onClick(View v) {
                  if(lastClickedButton == v){
                      //stuff
                      return;
                  }
                  //stuff
                  mediaPlayer.play(pathAudio2); //play sound, same problem
              }
          });
  
         //goes on like this some more times...
      }
  
      public void logIn(View v, String right){
          //stuff
  
          if(right.equals("true")){
             //stuff
              try {
                  AssetFileDescriptor descriptor = getAssets().openFd("Right");
                  mediaPlayer.play(descriptor); //play sound from assets
                  Intent myIntent = new Intent(getApplication(), playGame.class);
                  startActivity(myIntent);
              }catch (IOException e){
              }
          }else{
            //stuff
              try {
                  AssetFileDescriptor descriptor = getAssets().openFd("Wrong");
                  mediaPlayer.play(descriptor); //play sound from assets
                  Intent myIntent = new Intent(getApplication(), playGame.class);
                  startActivity(myIntent);
              }catch (IOException e){
              }
          }
      }
  }
  </code></pre>
  
  <p>The app plays the first sound as mentioned in my comments in my code. When I click a button which should start another sound I get the following error:</p>
  
  <pre><code>03-16 23:07:38.478  13646-13646/com.example.cello.myownquiz E/AndroidRuntime﹕ FATAL EXCEPTION: main
      java.lang.IllegalStateException
              at android.media.MediaPlayer.setDataSource(Native Method)
              at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1005)
  </code></pre>
  
  <p>Is my MediaPlayer class okay or am I missing something depending on the state? Is this class the only spot to call release or do I have to put it somewhere in my activity, too?</p>
  
  <p>Does anybody see my mistake? This problem with the mediaPlayer took me a whole day of trying, hope you can help me...</p>