4

How can I make as long as the app is opened, the users shouldn't be allowed to change the volume of the device?, if this is possible.

I have found that you can set the volume to mute with AudioManger:

AudioManager volumeControl = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
volumeControl.setStreamMute(AudioManager.STREAM_MUSIC, true);

However this is not what I'm looking for. I want when the user enters the application to lock its current volume and does not allow to change it, while in background a melody is playing increasing the volume. (this is the reasons for why I don't want let user control the volume)

I was thinking if there's a possibility to override the Volume up, Volume down keys? just like we can override the Back button.

VCODE
  • 535
  • 5
  • 19
  • 6
    Sounds annoying. Why do you know better than your users? – sarnold May 28 '12 at 22:58
  • 4
    I would uninstall it instantly if it makes sounds that I'm not able to mute / decrease in volume, no matter what the application is. – Mihai Todor May 28 '12 at 23:01
  • You can't override this, thankfully, for the same reasons you can't control the platform. – Kristopher Micinski May 28 '12 at 23:09
  • Not the kind of app I would want. Your app should never override my wishes. – mah May 28 '12 at 23:12
  • I wouldn't say that is a very good reason to prevent the user from changing the volume... – Alex Lockwood May 28 '12 at 23:24
  • 1
    Guys, guys, calm down. I perfectly understand you and agree, but the app that I'm developing is kind special. It tests the hearing loss of the users. And the idea is that while the tone is playing in background at a specific frequency and increasing the sound intensity, the user should not be able to change the volume of device, otherwise the results of the test would be wrong. – VCODE May 29 '12 at 19:50
  • Seems like a nifty application. I wish you the best. – FoamyGuy May 29 '12 at 20:03

1 Answers1

7

I was thinking if there's a possibility to override the Volume up, Volume down keys? just like we can override the Back button.

Yes, you can handle it in the same way.

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_VOLUME_DOWN)){
        //Nothing
    }
    return true;
}

@sarnold I can see the use case if it is an alarm clock. Many people want this feature in alarm clocks to stop themselves from turning the sound off when they are wanting to wake up.

Either way, be aware that you may annoy your users doing things like this. It should be avoided if at all possible.

EDIT: Setting the Volume

    AudioManager am =  (AudioManager) getSystemService(AUDIO_SERVICE); 
    am.setStreamVolume(AudioManager.STREAM_MUSIC,6,0);
FoamyGuy
  • 46,603
  • 18
  • 125
  • 156
  • Thank you a lot. This definitely helped me. (I wrote a comment above why I need this functionality.) – VCODE May 29 '12 at 19:59