Disable sound when volume keys clicked - java

In my application I override volume up and volume down keys. The problem is that when user click one of these two keys, sound is played. I want somehow to disable/mute this sound.
Here is fragment of my code:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_VOLUME_DOWN: {
// do something when user click volume down key
return true;
}
case KeyEvent.KEYCODE_VOLUME_UP: {
// do something when user click volume up key
return true;
}
}
return super.onKeyDown(keyCode, event);
}

Using onKeyDown or onKeyUp still gives the default sound of changing the volume. Use dispatchKeyEvent instead.
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
int keyCode = event.getKeyCode();
switch (keyCode) {
case KeyEvent.KEYCODE_VOLUME_UP:
case KeyEvent.KEYCODE_VOLUME_DOWN:
return true;
default:
return super.dispatchKeyEvent(event);
}
}

I figured it out for myself.
There is a way to stop the sound when volume key is pressed. You need to play an empty sound infinitely, so when user click a volume key it is "trying" to change the media volume, so there isn't click sound played.
You can download an empty sound by searching for empty sound and then put it in your raw folder in the project.
The last step is just to override the volume keys.
Here is the code you need:
Define MediaPlayer variable.
private MediaPlayer infinite_sound;
In onResume() you need to play the empty sound infinitely.
#Override
protected void onResume() {
super.onResume();
infinite_sound = MediaPlayer.create(this, R.raw.empty);
infinite_sound.setLooping(true);
infinite_sound.start();
}
In onPause() you need to stop the sound.
#Override
public void onPause() {
super.onPause();
infinite_sound.release();
}
And finally overriding the actions of the buttons.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_VOLUME_DOWN: {
// Do something when volume down button is clicked
return true;
}
case KeyEvent.KEYCODE_VOLUME_UP: {
// Do something when volume up button is clicked
return true;
}
}
return super.onKeyDown(keyCode, event);
}

Related

How to handle different keyboard action in Anrdoid app?

I have attached a physical keyboard to an Android tablet using USB. How can I call a function or perform some action in an Android app on pressing a paticular key, for example when pressing the zero key?
How could I implement the below code (for the zero key)?
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_A:
{
// your Action code
return true;
}
}
return super.onKeyDown(keyCode, event);
}
It was such a silly mistake i didnt import Keyevent ..

Custom hardware button events/intents

I have a custom device with a custom button and I need to handle an hardware Button Events/Intents:
every time I press the button it generates a PTT Press Action and I need to open my custom application, is there a way to do this?
If by custom device, you mean custom AOSP. Then make sure, it's button bound to events, this should be done with low level device driver configuration. And it's complicated work. Next, make sure you receive your click in next events.
Try to either use dispatch key event on Activity work:
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
if (event.getAction() == KeyEvent.ACTION_UP){
enter();
return true;
}}
return super.dispatchKeyEvent(event);
};
On on key event with Android View which is in focus.
public boolean onKey(View v, int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_ENTER:
/* This is a sample for handling the Enter button */
return true;
}
return false;
}

How does the app still work when pausing the app?

This is my code when I press the volume up button to next / prev songs :
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
prevSongs();
return true;
} else if ((keyCode == KeyEvent.KEYCODE_VOLUME_UP)) {
nextSongs();
return true;
} else return super.onKeyDown(keyCode, event);
}
But when the application exits (with the home button to return to the home screen) and I press the volume up button again, it does not work.
How does it work whether it's in or out of the app?
Thank !
You need to create a service to handle your playing media. When you exit the app, you can't listen to volume events, but in service you can do it. Also, listening to volume keys to change songs is a bad idea. You should use a notification with actions (buttons).

android - how to run some code after the "power button" is pushed

I have an app that plays some music file while the application is open but when i press the power button the sound keeps playing for some reason. Is there a way to override the power button so it will also stop the media player when pushed ?
MediaPlayer mp;
protected void onCreate(Bundle savedInstanceState) {
...
...
mp = MediaPlayer.create(this, mAudio[0]);
mp.start();
...
...
}
I would like to call mp.stop() when the power button is pressed.
Override onKeyDown and check if it's KEYCODE_POWER.
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_POWER) {
// Stop the media player here
return true;
}
return super.onKeyDown(keyCode, event);
}
Add this method:
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_POWER) {
// stop the media from playing
return true;
}
return super.onKeyDown(keyCode, event);
}
This method is being activated when a key is being pressed. It checks what key is pressed and reacts for it.
First of all you need to understand, when you want to pause or stop the sound. If you want to do it, when you exit/hide an application, then overriding onKeyDown won't help. I think better approach in this case will be:
protected void onCreate(Bundle onSaveInstanceState) {
super.onCreate(onSaveInstanceState);
mp = MediaPlayer.create(this, mAudio[0]);
}
protected void onStart() {
super.onStart();
mp.start();
}
protected void onStop() {
super.onStop();
mp.stop();
}
But if you want to stop your sound only when Power button is pressed, then yes, you need to override onKeyDown

on OptionsMenu click listener

I simply want to the options menu to toggle my sliding menu but havent found the standard click listener for the options button. Is OnPrepareOptionsMenu the only method that fires when clicking the options button? I don't really want to use this method because this method also gets fired when the application starts up.
UPDATE: for the menu button (hardware button), you can use the event onKeyUp:
public boolean onKeyUp(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_MENU) {
if (event.getAction() == KeyEvent.ACTION_UP)
{
Log.d("onkeyup", "onkeyup");
return true;
}
}
return super.onKeyUp(keyCode, event);
}
NOTE: the solution below is for the ActionBar
Just override the onOptionsItemSelected function.
You can then listen the click event on all the menu items and trigger the actions you want according to the case:
Here is an example from a main activity class:
#Override
public boolean onOptionsItemSelected(MenuItem item){
switch(item.getItemId()) {
case R.id.menu_calendar:
makeToast("Loading...");
openCalendar();
break;
case R.id.menu_search:
makeToast("Loading...");
openSearch();
break;
case R.id.menu_settings:
openSettings();
break;
case R.id.menu_help:
openHelp();
break;
case R.id.menu_about:
mBackupManager.dataChanged();
openAbout();
break;
}
return true;
}
You need to Override menu button, so that when user clicks Menu your code would be executes instead of opening menu.Like this
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ( keyCode == KeyEvent.KEYCODE_MENU ) {
//Put the code for an action menu from the top here
return true;
}
return super.onKeyDown(keyCode, event);
}

Categories