title: android - how to make a button click play a sound file every time it been pressed?
body: <p>I have open a new project -</p>
  
  <p>Now what I would like to do is this -
  By pressing on the button I want an mp3 file being played - and also that each time the button is pressed than the sound file will start playing from the start of it once again - so let's say that the mp3 is 10 sec long, and I pressed the button and it's playing and after 4 sec I pressed the button again than the sound will be played again. </p>
  
  <p>Now what I would like to know is-
  1- Where should I put the mp3 file?</p>
  
  <p>2-what code do I have to add in order that when the button is pressed than the mp3 file will be played (let's call the mp3 file click_sound.mp3)?</p>
  
  <p>3- What I need to add to the code in order that the sound will be played again each time I will pressed the button?</p>
  
  <p>This is the code of the MainActivity.java -</p>
  
  <pre><code>package com.example.test1;
  
  import android.os.Bundle;
  import android.app.Activity;
  import android.view.Menu;
  
  public class MainActivity extends Activity {
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
      }
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          // Inflate the menu; this adds items to the action bar if it is present.
          getMenuInflater().inflate(R.menu.main, menu);
          return true;
      }
  
  }
  </code></pre>
  
  <p>and this is the activity_main.xml</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/button1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="@string/play" /&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>