This is the most important part in the code presented in the original post.
Button one = (Button) this.findViewById(R.id.button1); final MediaPlayer mp = MediaPlayer.create(this, R.raw.soho); one.setOnClickListener(new OnClickListener(){ public void onClick(View v) { mp.start(); } });
To explain this step by step:
Button one = (Button) this.findViewById(R.id.button1);
First comes the initialization of the button, which will be used when playing sound. We use the Activity findViewById , passing the Id that we assigned to it (in this example: R.id.button1 ) to get the R.id.button1 button. We threw it as a Button , so it's easy to assign it to the one variable that we initialize. An explanation of how this works is beyond the scope of this answer. This gives a brief idea of โโhow this works.
final MediaPlayer mp = MediaPlayer.create(this, R.raw.soho);
This is how to initialize MediaPlayer . MediaPlayer follows the static factory method design pattern . To get an instance, we call its create() method and pass it the context and identifier of the sound resource that we want to play, in this case R.raw.soho . We declare this as final . John Skeet provided an excellent explanation of why we are doing this here .
one.setOnClickListener(new OnClickListener(){ public void onClick(View v) {
Finally, we set what our previously initialized button will do. Play sound at the touch of a button! To do this, we set the OnClickListener our one button. Inside there is only one method, onClick() which contains the instructions that the button should do when clicked.
public void onClick(View v) { mp.start(); }
To play the sound, we call the MediaPlayer start() method. This method starts sound playback.
There you can play the sound at the touch of a button in Android!
Bonus part:
As noted in the comment below, thanks Langusten Gustel! and, as recommended in the Android Developer's Reference Guide , it is important to call the release() method to free resources that will no longer be used. This is usually done after playback of the sound being played is completed. To do this, we add OnCompletionListener to our mp as follows:
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { public void onCompletion(MediaPlayer mp) {
Inside the onCompletion method, we release it like this:
public void onCompletion(MediaPlayer mp) { mp.release(); }
Obviously, there are better ways to implement this. For example, you can make MediaPlayer a class variable and process its life cycle along with the life cycle of the Fragment or Activity that uses it. However, this is a topic for another question. To keep this answer short, I wrote it just to illustrate how to play sound when a button is pressed in Android.
Original post
The first. You have to put your statements in a block, in which case the onCreate method.
Secondly. You initialized the button as a variable unit , then used the variable zero and set its onClickListener to an incomplete onClickListener. Use variable one for setOnClickListener.
Third, place the logic to play the sound inside onClick.
Eventually:
import android.app.Activity; import android.media.MediaPlayer; import android.os.Bundle; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class BasicScreenActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_basic_screen); Button one = (Button)this.findViewById(R.id.button1); final MediaPlayer mp = MediaPlayer.create(this, R.raw.soho); one.setOnClickListener(new OnClickListener(){ public void onClick(View v) { mp.start(); } }); } }