How to catch that volume was changed? - android

I use SeekBar for setting volume. I do the following:
I get current volume by getStreamVolume() and pass it to SeekBar element by setProgress().
I listen messages from the SeekBar element by onStopTrackingTouch() and set volume by setStreamVolume().
It works.
But user can change volume by hardware volume controls. I'd like to catch it and change SeekBar pointer position appropriately. How can I do it?

Try the following
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN || keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
//set your SeekBar
return true;
}
return super.onKeyDown(keyCode, event);
}
The question/answer that biegleux pointed you to is only for the "media keys" on an attached headset, not for the hardware volume up/down keys on the phone itself.

Related

Android : How to set the seekbar progress on increase/decrease of device volume and vice versa.?

In an android application, I want to set the device volume to the seekbar. Change in device volume has to change the seekbar progress and vice versa. Can anybody please help me to deal with this.?
update 1 :
This is what I have tried
Initialize AudioManager
AudioManager mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
Set device volume using
if (mAudioManager != null) {
mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, volume, 0);
//volume - progress value from seekbar
}
Now this code updates the device's volume, if there is a change in seekbar value. But how do I update the seekbar value, if there is a change in device's volume.?
This is how I solved the issue..
Overide onKeyDown() method in your activity.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// if one of the volume keys were pressed
if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
// change the seek bar progress indicator position
decreaseSeekbarValue();
} else if(keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
// change the seek bar progress indicator position
increaseSeekbarValue();
}
// propagate the key event
return super.onKeyDown(keyCode, event);
}
Try this :
AudioManager manager = (AudioManager) getSystemService(AUDIO_SERVICE);
MediaButton_Receiver mediaReceiver = new MediaButton_Receiver();
registerMediaButtonEventReceiver(mediaReceiver );
class MediaButton_Receiver implements BroadcastReceiver {
void onReceive(Intent intent) {
KeyEvent ke = (KeyEvent)intent.getExtra(Intent.EXTRA_KEY_EVENT);
if (ke .getKeyCode() == KeyEvent.KEYCODE_VOLUME_DOWN || ke .getKeyCode() == KeyEvent.KEYCODE_VOLUME_UP) {
// set the device volume
}
}
}
I think it should help you :)

onKeyDown has not been called when popupwindow is visible

I'm using the following function (in Activity) to detect volume buttons clicks:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN || keyCode == KeyEvent.KEYCODE_VOLUME_UP){
if(mAudioManager != null){
int curVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
Intent intent = new Intent();
intent.setAction(VerticalSeekBar.ACTION_VOLUME_CHANGED);
intent.putExtra(VerticalSeekBar.ARGUMENT_VOLUME_VALUE,curVolume);
sendBroadcast(intent);
return true;
}
}
return super.onKeyDown(keyCode, event);
}
Thing is I have popupwindow which represents volume bar as Vertical seek bar in it's view.
when the popupwindow is shown the onKeyDown in my activity is not being called untill I dimissed the popupwindow.
any suggestion how to solve this issue? is there another way to detect volume buttons clicks? thanks very much for the help
Try set focusable false for your PopupWindow class.
mMyPopupWindow.setFocusable(false);
Not sure how you are actually using that Popup but if you use a Dialog instead you have access to the onKeyDown(); for the Dialog.
http://developer.android.com/reference/android/app/Dialog.html#onKeyDown%28int,%20android.view.KeyEvent%29

Disable device volume controls

I created customized media player to play videos. i have volume button if click that button i just call device volume settings like this
AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,audioManager.getStreamVolume(AudioManager.STREAM_MUSIC), AudioManager.FLAG_SHOW_UI);
in my media player settings if i set disable volume control i don't want to show volume even click device side volume up/down button.
Any help is greatly appreciated!!!!!!
you can try overriding the volume key event
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN
|| keyCode == KeyEvent.KEYCODE_VOLUME_UP)
return true;
else
return false;
}
To help answer your intended outcome behaviour; 'i don't want to show volume even click device side volume up/down button'
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
switch( event.getKeyCode() ) {
case KeyEvent.KEYCODE_VOLUME_UP:
// do something
break;
case KeyEvent.KEYCODE_VOLUME_DOWN:
// do something
break;
default:
super.dispatchKeyEvent(event);
break;
}
return true;
}
will make it so that the volume does not get changed when you press down.
You can Try this Statement
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,audioManager.getStreamVolume(AudioManager.STREAM_MUSIC), AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
instead of
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,audioManager.getStreamVolume(AudioManager.STREAM_MUSIC), AudioManager.FLAG_SHOW_UI);

Android VideoView: How to receive volume up/down indications in the app

I am playing a video in my app using VideoView. When I press Volume up/down buttons on the phone, the Media Volume is changed. Instead of that I want to receive notifications (methods OR intents etc.) so that my app can do customized action when Volume up/down is pressed.
Try this code, to hack volume key
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN || keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
//Do your thing what you want...
return true;
} else {
return super.onKeyDown(keyCode, event);
}
}

seekbar thumb position change when user press device volume keys

Im using seek bar to control volume of the device. Im able to change the volume of the device using thumb of the seek bar just by dragging it on touch pad.
But when user presses volume(side) keys i need to set seek bar thumb position accordingly.
Hw i can do this please let me know
Thanks
I got solution, by overriding onkeydown event.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_VOLUME_UP)
{
int index = seekbar.getProgress();
seekbar.setProgress(index + 1);
return true;
} else if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN)
{
int index = seekbar.getProgress();
seekbar.setProgress(index - 1);
return true;
}
return super.onKeyDown(keyCode, event);
}
Edited for further reference

Categories

Resources