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.
Related
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
I'm having trouble with the following:
I have a spinner with different songs in it. In the spinner, users can preview the selected sound. I already developed that part. But my problem is: when I select one song from the list it will play. Then when we select another song from the list, Mediaplayer doesn't stop and plays the previous song also. But what I need is to stop the previous song when a user selects another song.
Here is my code...
//set onClickListner to the onItem SelectedListner
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
choose_ringtone = (int) id;
// Toast.makeText(setAlarm.this,"The selected choice is "+ id,Toast.LENGTH_SHORT).show();
String ringtSound = String.valueOf(parent.getItemAtPosition(position));
//set ringtone options
switch (ringtSound) {
case "alarm Sound 1":
mp = MediaPlayer.create(setAlarm.this, R.raw.wake_up);
break;
case "alarm Sound 2":
mp = MediaPlayer.create(setAlarm.this, R.raw.alarm);
break;
case "alarm Sound 3":
mp = MediaPlayer.create(setAlarm.this, R.raw.wake_up_tone);
break;
case "alarm Sound 4":
mp = MediaPlayer.create(setAlarm.this, R.raw.sweet_alarm);
break;
case "alarm Sound 5":
mp = MediaPlayer.create(setAlarm.this, R.raw.morning_alarm);
break;
default:
break;
}
if(mp!=null) {
mp.start();
}
Before the switch statement, you could do something like
if (mp != null && mp.isPlaying()) {
mp.stop();
}
I am not sure what the rest of your code looks like, but please make sure that it also follows the tips from Android Developers, specifically:
It is also recommended that once a MediaPlayer object is no longer being used, call release() immediately so that resources used by the internal player engine associated with the MediaPlayer object can be released immediately. Resource may include singleton resources such as hardware acceleration components and failure to call release() may cause subsequent instances of MediaPlayer objects to fallback to software implementations or fail altogether. Once the MediaPlayer object is in the End state, it can no longer be used and there is no way to bring it back to any other state.
Try this,,Before start Play call below function.
switch (ringtSound) {
case "alarm Sound 1":
stopPlaying()
mp = MediaPlayer.create(setAlarm.this, R.raw.wake_up);
mp.start();
break;
}
Call the function to stop sound.
private void stopPlaying() {
if (mp != null) {
mp.stop();
mp.release();
mp = null;
}
}
Try mp.stop(); before start any sound...
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.
I noticed that regardless of the volume I set my phone, the sound for a button click in my app remains the same regardless, how can I synchronize it with the phone's system volume? Here's the code format I used
Mediaplayer buttonSoundClick;
buttonClickSound = MediaPlayer.create(this, R.raw.button_click_sound);
buttonClickSound.start();
buttonClickSound = MediaPlayer.create(this, R.raw.button_click_sound);
} private void prepareAsync() {
buttonClickSound.setAudioStreamType(AudioManager.STREAM_MUSIC);
}
#Override
public void onClick(View v) {
//Button Click Sound
buttonClickSound.start();
Android has different volume levels for different stream types.
Override onKeyDown() and add the following code.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
AudioManager audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
switch (keyCode) {
case KeyEvent.KEYCODE_VOLUME_UP:
audio.adjustStreamVolume(AudioManager.STREAM_MUSIC,
AudioManager.ADJUST_RAISE, AudioManager.FLAG_SHOW_UI);
return true;
case KeyEvent.KEYCODE_VOLUME_DOWN:
audio.adjustStreamVolume(AudioManager.STREAM_MUSIC,
AudioManager.ADJUST_LOWER, AudioManager.FLAG_SHOW_UI);
return true;
default:
return false;
}
}
After this, try playing around with the stream types to get it right for your app. The stream types available are:
AudioManager.STREAM_MUSIC
AudioManager.STREAM_RING
AudioManager.STREAM_ALARM
AudioManager.STREAM_SYSTEM
AudioManager.STREAM_NOTIFICATION
OR
You could directly set the MediaPlayer stream type by using MediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC)
Buy, you must call this method before prepare() or prepareAsync() in order for the target stream type to become effective thereafter.
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.