Play sound when you click the mouse

How to get a button to play sound from raw when pressed? I just created a button with id button1 , but no matter what I write, everything is wrong.

 import android.media.MediaPlayer; 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); MediaPlayer = mp; mp = MediaPlayer.create(this, R.raw.soho); zero.setOnCliclListener(new View.OnClickListener() ) @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.basic_screen, menu); return true; } } 
+89
android click audio android-mediaplayer playback
Aug 27 '13 at 7:08
source share
10 answers

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) { //code } }); 

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) { //code } }); 

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(); } }); } } 
+189
Aug 27 '13 at 7:21
source share

Tested and working 100%

 public class MainActivity extends ActionBarActivity { Context context = this; MediaPlayer mp; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_layout); mp = MediaPlayer.create(context, R.raw.sound); final Button b = (Button) findViewById(R.id.Button); b.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { try { if (mp.isPlaying()) { mp.stop(); mp.release(); mp = MediaPlayer.create(context, R.raw.sound); } mp.start(); } catch(Exception e) { e.printStackTrace(); } } }); } } 



That is all we had to do.

 if (mp.isPlaying()) { mp.stop(); mp.release(); mp = MediaPlayer.create(context, R.raw.sound); } 
+34
Aug 25 '15 at 14:38
source share

The best way to do it here, I found after searching for one problem after another in LogCat

 MediaPlayer mp; mp = MediaPlayer.create(context, R.raw.sound_one); mp.setOnCompletionListener(new OnCompletionListener() { @Override public void onCompletion(MediaPlayer mp) { // TODO Auto-generated method stub mp.reset(); mp.release(); mp=null; } }); mp.start(); 

Not releasing Media Player gives you this error in LogCat:

Android: MediaPlayer completed without exiting

Without resetting Media Player, you get this error in LogCat:

 Android: mediaplayer went away with unhandled events 

So play the safe and easy code to use the media player.

To play more than one sound in one action / fragment, just change resID when creating a new media player, for example

 mp = MediaPlayer.create(context, R.raw.sound_two); 

and play it!

Have some fun!

+30
Jul 04 '14 at 8:26
source share
 import android.media.MediaPlayer; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; import android.widget.Button; public class MainActivity extends Activity { MediaPlayer mp; Button one; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mp = MediaPlayer.create(this, R.raw.soho); one = (Button)this.findViewById(R.id.button1); one.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub mp.start(); } }); } } 
+8
Aug 27 '13 at 7:18
source share
  • Audio should be placed in the raw folder, if it does not exist, create it.
  • The raw folder must be inside the res folder
  • The name should not be - or special characters in it.

In your activity, you must have a MediaPlayer object, inside the onCreate method or onclick method, you must initialize the MediaPlayer , for example MediaPlayer.create(this, R.raw.name_of_your_audio_file) , then your audio file is ready to work. in your case, when you call the start() method, since you want it to be placed in the button, you have to put it in the onClick method.

Example:

 private Button myButton; private MediaPlayer mp; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.myactivity); mp = MediaPlayer.create(this, R.raw.gunshot); myButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mp.start(); } }); } } 
+5
Aug 10 '18 at 19:12
source share
 Button button1=(Button)findViewById(R.id.btnB1); button1.setOnClickListener(new OnClickListener(){ public void onClick(View v) { MediaPlayer mp1 = MediaPlayer.create(this, R.raw.b1); mp1.start(); } }); 

Try this, I think it will work

+1
Dec 10 '17 at 18:58
source share
 public class MainActivity extends AppCompatActivity { public void clickMe (View view) { MediaPlayer mp = MediaPlayer.create(this, R.raw.xxx); mp.start(); } 

create a button with a method can be called when the button is pressed (onCreate),

then create a variable for the class (MediaPlayer) with the path of your file

 MediaPlayer mp = MediaPlayer.create(this, R.raw.xxx); 

finally run the launch method in this class

 mp.start(); 

the file will work when the button is pressed, hope it was useful!

+1
May 16 '18 at 15:24
source share

There are several predefined sounds: SHUTTER_CLICK, FOCUS_COMPLETE, START_VIDEO_RECORDING, STOP_VIDEO_RECORDING.

Nice!

MediaActionSound

A class for creating sounds corresponding to sounds created by various actions performed by the media and camera APIs. Documents

use as:

 fun playBeepSound() { val sound = MediaActionSound() sound.play(MediaActionSound.START_VIDEO_RECORDING) } 
+1
Feb 12 '19 at 18:09
source share

Instead of resetting as suggested by DeathRs:

 if (mp.isPlaying()) { mp.stop(); mp.release(); mp = MediaPlayer.create(context, R.raw.sound); } mp.start(); 

we can just reset MediaPlayer to start using:

 if (mp.isPlaying()) { mp.seekTo(0) } 
0
Jul 24 '19 at 13:58
source share

All these solutions โ€œsoundโ€ beautiful and reasonable, but there is one big drawback. What happens if your client downloads your application and presses your button several times?

Your MediaPlayer sometimes does not play your sound if you press a button many times.

I ran into this MediaPlayer class performance issue a few days ago.

MediaPlayer class MediaPlayer to use? Not always. If you have short sounds, it is better to use the SoundPool class.

An economical and efficient solution is the SoundPool class, which offers great features and improves the performance of your application.

SoundPool is not as easy to use as the MediaPlayer class, but it has some great advantages when it comes to performance and reliability.

Follow this link and find out how to use the SoundPool class in your application:

Save decision

-one
Jun 25 '19 at 15:40
source share



All Articles