Disable Volume Keys

All your problems with Audio, Video and Images.

Disable Volume Keys

Postby matany » Sun Jan 10, 2010 6:30 pm

Hi,

Can someone please tell me how to disable the volume keys in my code.

It crashes my app if you increase the volume.

Thanks

Mat
matany
Experienced Developer
Experienced Developer
 
Posts: 66
Joined: Tue Dec 08, 2009 9:29 pm

Top

Postby Fonceur » Sun Jan 10, 2010 9:01 pm

You could try something like;

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. public boolean onKeyDown(int keyCode, KeyEvent event)
  3.  
  4. {
  5.  
  6.    if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN ||
  7.  
  8.       keyCode == KeyEvent.KEYCODE_VOLUME_UP)
  9.  
  10.    return true;
  11.  
  12.    else return super.onKeyDown(keyCode, event);
  13.  
  14. }
Parsed in 0.031 seconds, using GeSHi 1.0.8.4
Fonceur
Junior Developer
Junior Developer
 
Posts: 18
Joined: Sun Jan 10, 2010 8:53 pm

Postby matany » Sun Jan 10, 2010 9:08 pm

Thank you so much,

But where should I call this method and where can I get the values of the args.

Thanks

Mat
matany
Experienced Developer
Experienced Developer
 
Posts: 66
Joined: Tue Dec 08, 2009 9:29 pm

Postby Fonceur » Sun Jan 10, 2010 9:14 pm

You would just define it for the activity where using the volume is a problem... All it does is intercept those hardware button presses and eat them, instead of passing them to the super.onKeyDown...

Of course, ideally you'd want to figure out why pressing the volume buttons is bad... ;)
Fonceur
Junior Developer
Junior Developer
 
Posts: 18
Joined: Sun Jan 10, 2010 8:53 pm

Postby matany » Sun Jan 10, 2010 9:27 pm

Ok, great, thank you.

So I need to disable the keys before I play my sound and enable them back after I am done...

How?

Sorry for asking basic questions.

Thanks

Mat
matany
Experienced Developer
Experienced Developer
 
Posts: 66
Joined: Tue Dec 08, 2009 9:29 pm

Postby Fonceur » Sun Jan 10, 2010 9:39 pm

Well, the code snippet intercepts all the hardware button presses... If you only want to disable during playback, and that you're not using a dedicated activity for that, then you could add an extra flag (like "boolean playing = true;", and use it in the if condition). If your application isn't too long, it might be easier to just post the code...
Fonceur
Junior Developer
Junior Developer
 
Posts: 18
Joined: Sun Jan 10, 2010 8:53 pm

Top

Postby matany » Sun Jan 10, 2010 9:45 pm

Thanks Fonceur

The code you gave me, I placed it in my activity and when I press the volume, it still crashes the program, but where could you call that method (onKeyDown)?

The method is pasted under the onCreate, but where do you call it to activate it???

Thanks

Mat
matany
Experienced Developer
Experienced Developer
 
Posts: 66
Joined: Tue Dec 08, 2009 9:29 pm

Postby matany » Sun Jan 10, 2010 9:54 pm

Ok...


I GOT IT....

I needed KeyDown and KeyUP methods at the same time...

Like...

public boolean onKeyDown(int keyCode, KeyEvent event)
{

if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN ||
keyCode == KeyEvent.KEYCODE_VOLUME_UP)
return true;
else return super.onKeyDown(keyCode, event);
}

public boolean onKeyUp(int keyCode, KeyEvent event)
{

if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN ||
keyCode == KeyEvent.KEYCODE_VOLUME_UP)
return true;
else return super.onKeyDown(keyCode, event);
}



Thank you sooooooooooooooooo much.....

YOU SAVED ME ALOT..

Mat
matany
Experienced Developer
Experienced Developer
 
Posts: 66
Joined: Tue Dec 08, 2009 9:29 pm

Postby Fonceur » Sun Jan 10, 2010 9:56 pm

You don't need to call it anywhere, it's done automatically for you... You could add some debug printing in there to convince yourself... Too bad that didn't seem to help.
Fonceur
Junior Developer
Junior Developer
 
Posts: 18
Joined: Sun Jan 10, 2010 8:53 pm

Top

Return to Multimedia Problems

Who is online

Users browsing this forum: No registered users and 8 guests