VCODE
VCODE

Reputation: 700

Android - Prevent people to change the volume

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.

Upvotes: 4

Views: 2980

Answers (1)

FoamyGuy
FoamyGuy

Reputation: 46856

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

Upvotes: 7

Related Questions