How to start and stop a sound in android? - android

I have a button called " micro" , when I click on my sound is played , and when I click on again it must stop
I tried the code below but when I click on my button for the second time , the music is played again and again without stopping:
Button micro=(Button)findViewById(R.id.micro);
micro.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
MediaPlayer mp = MediaPlayer.create(getBaseContext(),
R.raw.mymusic);
mp.start();
}
if(event.getAction() == MotionEvent.ACTION_UP){
MediaPlayer mp = MediaPlayer.create(getBaseContext(),
R.raw.mymusic);
mp.stop();
}
return true;
}
});

You are creating separate MediaPlayer objects. You should just create one that you tell to start and stop as necessary.
Right now you're telling one to start, and then later telling a different one to stop.

Related

Problems with audio in onTouchListener and mediaplayer.setLooping

im trying to do a Soundboard for a school proyect, but I have problems when I pulse the buttons, If I pulse a button twice, the sound does not reproduce again, and, even if the condition fullfills, the audio doesnt loop. I have different buttons, and all of they call a method (each one with different parameters on the call
btn1.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
reproducir(mp1, motionEvent, btn1, colorin);
return false;
}
});
public void reproducir(MediaPlayer mp, MotionEvent motionEvent, Button btn, String colorin ){
switch(motionEvent.getAction()){
case MotionEvent.ACTION_DOWN:
mp.start();
btn.setBackgroundColor(getResources().getColor(R.color.Blanco));
if(instrumento.getText().equals("piano")) {
mp.setLooping(true);
}
break;
case MotionEvent.ACTION_UP:
mp.stop();
ponerColor(colorin);
}
}
The last line calls to a method to put again the original color of the button, thanks a lot
Your Code is Fine Only One tiny Mistake:
You should replace mp.stop(); with mp.pause();
That's All.
Better to it Like this though:
if (mp.isPlaying()){
mp.pause();
}
....
if (!mp.isPlaying()){
mp.start();
}
...
if (!mp.isLooping()){
mp.setLooping(true);
}
I should also let you know that mp.stop() Stops the media, it's Good, But Stop is only to be used in case You want the Whole MediaPlayer Forget what song is being played and we just wanna change The Audio, reset MEdia player and start another Audio.
Hope you find this useful

Keep a sound sample playing till button is released android

I am trying to implement a musical keyboard application that will play string sounds. String sounds are needed to be played till the user releases the key.
I am using a small sample of 1 second with the idea of looping the sample using SoundPool on
MotionEvent.ACTION_DOWN,
and stopping it as
ACTION_UP
gets called. But looping does not seem to work. I can put a "long enough" sound sample assuming no user would keep the button pressed for that long, but that is not quite the way I want the app work.
What should I do?
Try this code:
inside onCreate()
mp=MediaPlayer.create(MainActivity.this,R.raw.beep);
b.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
mp.setLooping(true);
mp.start();
}
if (event.getAction() == MotionEvent.ACTION_UP) {
mp.stop();
mp=MediaPlayer.create(MainActivity.this,R.raw.beep);
}
return false;
}
});

How to Keep volume fixed in MediaPlayer

I am using MediaPlayer to play a sound and below is my code
mediaPlayer = MediaPlayer.create(MainActivity.this, R.raw.sound);
mediaPlayer.setLooping(true);
mediaPlayer.setVolume(0.5f, 0.5f);
mediaPlayer.start();
the MediaPlayer plays the sound in the given volume but the volume can be increased or decreased using the hardware key. I want to know if there is a way to keep the volume for my mediaPlayer fixed no matter what volume the user keeps using the hardware key
Thanks in advance
AFASIK Media Player does not provide any such Listener. You need to override key press events for checking that
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
int action = event.getAction();
int keyCode = event.getKeyCode();
switch (keyCode) {
case KeyEvent.KEYCODE_VOLUME_UP:
if (action == KeyEvent.ACTION_DOWN) {
//TODO
}
return true;
case KeyEvent.KEYCODE_VOLUME_DOWN:
if (action == KeyEvent.ACTION_DOWN) {
//TODO
}
return true;
default:
return super.dispatchKeyEvent(event);
}
}
MediaPlayer provide isPlaying boolean to know either mediaPlayer is in use, you can use that in conjunction with KeyPress events.

How to start & stop a sound with a button?

I have a Button in my project.
I want when Button clicked>>>> start a sound
and when Button clicked again>>> stop the sound...
I use this code but can't stop the sound and start it again and again...
How can I do this?
Thank you.
Button btritm1 = (Button) findViewById(R.id.button9991);
btritm1.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
final MediaPlayer mp1_1 = MediaPlayer.create(MainActivity.this, R.raw.ritm1);
if (event.getAction() == MotionEvent.ACTION_DOWN )
{
if(mp1_1 != null && mp1_1.isPlaying() )
{
mp1_1.stop();
}
else {
// xritm1 = 1;
// snd.stop_s_ritm1();
mp1_1.setLooping(true);
mp1_1.start();
}
} // end of if
return false;
}
}); // end of ontouch listener/*
if(sound.isPlaying()){
sound.stop();
}else{
sound.reset();
sound.setDataSource(yourURL);//or InputStream etc.
sound.prepare();
sound.start();
}
To use a good practice, declare the MediaPlayer object as a class variable and initialize it in your onCreate() method. Your problem is that a new player object is created each time your control is touched.
So as was already said above, the line
final MediaPlayer mp1_1 = MediaPlayer.create(MainActivity.this, R.raw.ritm1);
should be outside the Listener.

Playing default android sound of button, clicking onTouch() method

In my android app I have some buttons, that should work with onTouch() method, course I need to change button's text, when finger in ACTION_DOWN position. But this buttons should to play default android sound of button clicking (like in onClick() method). Where I can find such sound? I think, it must be in SDK, but how can I find it? And what methods are better for such operation? I'm using MediaPlayer.
Listing:
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
addTextShadow(v);
Log.v(LOG_TAG, "ACTION_DOWN");
break;
case MotionEvent.ACTION_MOVE:
clearTextShadow(v);
isMoved = true;
Log.v(LOG_TAG, "ACTION_MOVE");
break;
case MotionEvent.ACTION_UP:
Log.v(LOG_TAG, "ACTION_UP");
if (!isMoved) {
clearTextShadow(v);
if (v.getId() == R.id.btn_new) {
Log.v(LOG_TAG, "New pressed");
playSound(); //MY METHOD TO PLAY SOUND
} else if (v.getId() == R.id.btn_saved) {
Log.v(LOG_TAG, "Saved pressed");
} else if (v.getId() == R.id.btn_random) {
Log.v(LOG_TAG, "Random pressed");
}
}
isMoved = false;
break;
}
return true;
}
And my playSound() method:
public void playSound(){
if (mp != null) {
mp.release();
mp = null;
}
try {
mp = MediaPlayer
.create(MyActivity.this, R.raw.sound); //BUT HERE I NEED DEFAULT SOUND!
mp.start();
} catch (Exception e) {
e.printStackTrace();
}
}
Use this code in your onTouch() method:
view.playSoundEffect(android.view.SoundEffectConstants.CLICK);
If you are simply looking for a preset Android sound then it's better to use this functionality that's provided to you for free by the framework. Doing anything else, unless necessary for customization, would simply result in you working against the framework instead of working with it.
Use default sounds from /system/media/audio/
MediaPlayer or SoundPool will help to implement playback.
Consider using view.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);.
It will do both: playing the default sound and vibrate if the user has it enabled in the global settings.

Categories

Resources