title: Android button click to play music, click again to stop sound
body: <p>I have a button, when I click it plays music, how to do it, when I click second time, to stop the music?</p>
  
  <pre><code> Button two = (Button)this.findViewById(R.id.button2);
              final MediaPlayer mp2 = MediaPlayer.create(this, R.raw.two);
              two.setOnClickListener(new OnClickListener(){
  
                  public void onClick(View v) {
                      mp2.start();
                  }
              });  
  </code></pre>
  
  <p>Ok, this one works:</p>
  
  <pre><code>Button one = (Button)this.findViewById(R.id.button1);
      final MediaPlayer mp1 = MediaPlayer.create(this, R.raw.n);
      one.setOnClickListener(new OnClickListener(){
  
          @Override
              public void onClick(View v) {
              if (mp1.isPlaying()) {
                  mp2.pause();
                  } 
                  else {
                  mp2.start();
              }
          ;
      }});
  </code></pre>
  
  <p>The one with Pause above it works, but If I want to stop the music, it does not work.
  Following not working:      </p>
  
  <pre><code>@Override
          public void onClick(View v) {
          if (mp1.isPlaying()) {
              mp2.stop();
              } 
              else {
              mp2.start();
          }
      ;
  }});
  </code></pre>
  
  <p><strong>I get error: start called in state 0<br>
  error (-38, 0)</strong></p>