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.
Related
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.
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.
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.
I'm developing a game and when the game is playing the volume control is not working. Can anyone give me the solution?
It is in AndEngine
I found the solution. Where the keycodes are handled return false and volume will start working. I was handling back button and returning true in all cases there. So what I did was that I returned true only when keypressed is back else return false and my problem solved.
There are four kind of sound setting in android 1)Alarm 2)Music 3)Ring Tone 4)Notification
First Create object of AudioManager amanager;
IF you want to set Volume use this code
For Notification
AudioManager amanager=(AudioManager)getSystemService(Context.AUDIO_SERVICE);
amanager.setStreamVolume(AudioManager.STREAM_NOTIFICATION, AudioManager.FLAG_SHOW_UI + AudioManager.FLAG_PLAY_SOUND);
For Alerm
amanager.setStreamVolume(AudioManager.STREAM_ALARM,AudioManager.FLAG_SHOW_UI + AudioManager.FLAG_PLAY_SOUND);
For Music
amanager.setStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.FLAG_SHOW_UI + AudioManager.FLAG_PLAY_SOUND);
For Ring Tone
amanager.setStreamVolume(AudioManager.STREAM_RING,AudioManager.FLAG_SHOW_UI + AudioManager.FLAG_PLAY_SOUND);
I override onKeyDown and not properly handling volume keys, my solution is this:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
boolean ret = super.onKeyDown(keyCode, event);
if (keyCode == KeyEvent.KEYCODE_VOLUME_UP
|| keyCode == KeyEvent.KEYCODE_VOLUME_DOWN
|| keyCode == KeyEvent.KEYCODE_VOLUME_MUTE) {
return ret;
}
return handleKeyDown(keyCode, event);
}
Using the code below I have stopped the use of the volume buttons unless I am streaming audio (otherwise it annoyingly changes the ringer volume), but the 'Back' button isn't working.
Pressing 'back' should got to my phones desktop (or exit my app, like you would expect), but it isn't doing anything. If I open the menu, 'Back' will close the menu as it should, but I can't leave the app.
I have copied the code onto other activities within my app, if I open another activity within my app, because the 'Back' button isn't working, I can't go back to the main screen :)
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
//Suppress the use of the volume keys unless we are currently listening to the stream
if(keyCode==KeyEvent.KEYCODE_VOLUME_UP) {
if(StreamService.INT_PLAY_STATE==0){
return true;
}else{
return false;
}
}
if(keyCode==KeyEvent.KEYCODE_VOLUME_DOWN) {
if(StreamService.INT_PLAY_STATE==0){
return true;
}else{
return false;
}
}
return false;
Why is this happening?
Haven't tested, but I think you need to include an else where you call super.onKeyDown, ie:
if(keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
code
} else if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
more code
} else {
super.onKeyDown(keyCode, event);
}
Otherwise, you're capturing all keycodes and returning false after checking the volume codes.
A simpler and more robust way to have the volume keys always control the Media volume is to insert this line into your Activity's onCreate():
setVolumeControlStream(AudioManager.STREAM_MUSIC);
Dude, just change the audio context on that activity to media volume:
http://developer.android.com/reference/android/media/AudioManager.html
EDIT:
private AudioManager audio;
Inside onCreate:
audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
Override onKeyDown:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
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;
}
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
//your code
return true;
} else if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
//your code
return true;
} else {
super.onKeyDown(keyCode, event);
}
return true;
}