title: Android studio OnTouchListener
body: <p>I am trying to create a app for music. It is being developed in Android Studio. I want this button to start the sound when I press down and stop when I release the button, but I can't make it work.</p>
  
  <pre><code>import android.media.MediaPlayer;  
  
  import android.os.Bundle;   
  
  import android.support.v7.app.AppCompatActivity;    
  
  import android.view.MotionEvent;   
  
  import android.view.View;  
  
  import android.widget.Button;   
  
  public class MainActivity extends AppCompatActivity {
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          final MediaPlayer minus5 = MediaPlayer.create(this, R.raw.min5);
  
          final Button play_min5 = (Button) this.findViewById(R.id.min5);
  
          play_min5.setOnTouchListener(new View.OnTouchListener() {
          @Override
          public boolean onTouch(View v, MotionEvent event) {
              switch (event.getAction()) {
                  case MotionEvent.ACTION_DOWN:
                      // PRESSED
                      minus5.start();
                      return true; // if you want to handle the touch event
                  case MotionEvent.ACTION_UP:
                      // RELEASED
                      minus5.stop();
                      return true; // if you want to handle the touch event
              }
              return false;
          }
      });
  
  }
  </code></pre>
  
  <p>}</p>