title: Multiple Sound Buttons in Android: Starting, Stopping and Looping sounds through buttons
body: <p>I'm new to Android programming and I am having a couple issues. I'm trying to make an app that has three buttons that all play different short sounds. I would like for the sounds to not overlap each other, so when I press one button, the sound from another stops. Here is my code.</p>
  
  <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:paddingBottom="@dimen/activity_vertical_margin"
  android:paddingLeft="@dimen/activity_horizontal_margin"
  android:paddingRight="@dimen/activity_horizontal_margin"
  android:paddingTop="@dimen/activity_vertical_margin"
  tools:context=".MainActivity" &gt;
  
  &lt;Button
      android:id="@+id/button2"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignLeft="@+id/button1"
      android:layout_below="@+id/button1"
      android:layout_marginTop="42dp"
      android:text="Button" /&gt;
  
  &lt;Button
      android:id="@+id/button1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentTop="true"
      android:layout_centerHorizontal="true"
      android:layout_marginTop="91dp"
      android:text="Button" /&gt;
  
  &lt;Button
      android:id="@+id/button3"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignLeft="@+id/button2"
      android:layout_below="@+id/button2"
      android:layout_marginTop="48dp"
      android:text="Button" /&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>.</p>
  
  <pre><code>package com.example.soundbutton;
  
  import java.io.IOException;
  
  import android.media.MediaPlayer;
  import android.os.Bundle;
  import android.app.Activity;
  import android.view.View;
  import android.widget.Button;
  
  public class MainActivity extends Activity {
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
  
        final MediaPlayer s1 = MediaPlayer.create(this, R.raw.trumpet);
        final MediaPlayer s2 = MediaPlayer.create(this, R.raw.sy04);
        final MediaPlayer s3 = MediaPlayer.create(this, R.raw.machine);
  
          Button b1 = (Button) findViewById(R.id.button1);
  
          b1.setOnClickListener(new Button.OnClickListener() {
          @Override
              public void onClick(View v) {
                  s2.stop();
                  s3.stop();
  
                  try {
                      s1.prepare();
                      s1.seekTo(0);
                      s1.start();
                  } catch (IllegalStateException e) {
                      // TODO Auto-generated catch block
                      e.printStackTrace();
                  } catch (IOException e) {
                      // TODO Auto-generated catch block
                      e.printStackTrace();
                  }
  
              }
          });
  
          Button b2 = (Button) findViewById(R.id.button2);
  
          b2.setOnClickListener(new Button.OnClickListener() {
              @Override
              public void onClick(View v) {
                  s1.stop();
                  s3.stop();
  
                  try {
                      s2.prepare();
                      s2.seekTo(0);
                      s2.start();
                  } catch (IllegalStateException e) {
                      // TODO Auto-generated catch block
                      e.printStackTrace();
                  } catch (IOException e) {
                      // TODO Auto-generated catch block
                      e.printStackTrace();
                  }
  
              }
          });
  
         Button b3 = (Button) findViewById(R.id.button3);
  
          b3.setOnClickListener(new Button.OnClickListener() {
              @Override
              public void onClick(View v) {
                  s2.stop();
                  s1.stop();
  
                  try {
                      s3.prepare();
                      s3.seekTo(0);
                      s3.start();
                  } catch (IllegalStateException e) {
                      // TODO Auto-generated catch block
                      e.printStackTrace();
                  } catch (IOException e) {
                      // TODO Auto-generated catch block
                      e.printStackTrace();
                  }
  
              }
          });
  
      }
  
      }
  </code></pre>
  
  <p>I have searched quite a bit on this and answers from similar issues have not worked for me. So far the app will not play the first button I press at all, but then allow you to play the other buttons. After a few clicks, all the buttons seize to work. I have heard SoundPool is ideal for short sounds but I couldn't find any sample code for what I'm trying to do. Anyway, I know there has to be a fix for this and I apologize if this is a trivial question. I would also appreciate info on how to loop the sounds for each button and still have the sounds from each button not overlap. Thanks for any and all help.</p>