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

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);
}
}

Related

Handle volume button when Option Menu is visible

From my activity, I correctly handled the opening on OptionMenu with this code :
public boolean onKeyDown(int keyCode, KeyEvent event){
if(KeyEvent.KEYCODE_VOLUME_UP == event.getKeyCode() || KeyEvent.KEYCODE_VOLUME_DOWN == event.getKeyCode()){
openOptionsMenu();
return true;
}else
return super.onKeyDown(keyCode, event);
}
But when the Option Menu is visible, I cannot intercept the KyDown anymore. My intend is to be able to close the Option Menu the same way I open it, with volume button.
Any suggestion ?

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);

How to catch that volume was changed?

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.

Handle event press hardware button Next and Pre?

I want to press hardware button Next will show Next Screen and press hardware button Pre will show Pre Screen (ViewFliper)
What do you know event press hardware button Next and Pre in Android ?
How catch event press hardware button Next and Pre in Android ?
You can watch image at here: enter link description here
Thanks!
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
mViewFliper.showPrevious();
return true;
}
if (keyCode == KeyEvent.KEYCODE_MENU) {
mViewFliper.showNext();
return true;
}
return super.onKeyDown(keyCode, event);
}
Although I can not express enough how utterly horrible it is for an app to do something like that.

How to access menu button onLongClick in Android?

How can I set a listener for long-click performed on hardware Menu button? What is the way of programmatic access to the Menu button?
Moreover how can I distinguish long-click from single-click? (As far as I know when long-click is performed the single-click event is propagated as well - I do not want this to happen because I need 2 different actions for these 2 situations. Long-click and single-click separate listeners for the device Menu button)
Thank you!
This shoule be fairly straight forward. Check out the KeyEvent.Callback on the Android developer's website.
There you will find onKeyLongPress() as well as onKeyDown() and onKeyUp(). This should get you on the right track. Comment or post you code if you need any further help.
Edit: I just re-read the question. If you are having trouble distinguishing single click from long click, you will need to use onKeyDown and onKeyUp and check the duration of the click. Esentially you will start a timer in the onKeyDown and check the time in the onKeyUp. You will have to watch for FLAG_CANCELED.
Further Edit: I found the time to do a couple of tests. This code should do what you want (onKeyUp() gets only short press events and onLongPress() gets only long press events).
The key thing here is in the call to event.startTracking() in the onKeyDown() handler.
Place in Activity (this should also work in a custom view as well but untested):
#Override
public boolean onKeyLongPress(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_MENU) {
// Handle Long Press here
Log.i("KeyCheck", "LongPress");
return true;
}
return super.onKeyLongPress(keyCode, event);
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
Log.i("KeyCheck", "KeyDown" + keyCode);
if (keyCode == KeyEvent.KEYCODE_MENU) {
event.startTracking(); //call startTracking() to get LongPress event
return true;
}
return super.onKeyDown(keyCode, event);
}
#Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_MENU && event.isTracking()
&& !event.isCanceled()) {
// handle regular key press here
Log.i("KeyCheck", "KeyUp");
return true;
}
return super.onKeyUp(keyCode, event);
}

Categories

Resources