1
votes

My app plays a sound while the device is locked (keyguard). I use Android MediaPlayer. The user can controll the sound's volume by pressing the hardware volume keys. I do not want the user to be able to change the volume while the device is locked. How can I disable the volume keys while device is locked?

I tried How to override the behavior of the volume buttons in an Android application?

dispatchKeyEvent and Onkeydown didn't work for me. I also tried setVolumeControlStream() but it didn't work. The user always can change the volume by pressing the volume key.

1

1 Answers

0
votes

Use the below :::::

AudioManager am = 
                    (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);

                am.setStreamVolume(
                    AudioManager.STREAM_MUSIC,
                    am.getStreamMaxVolume(AudioManager.STREAM_MUSIC),
                    0);

The above code set the volume to be maximum , even if you try to reduce the volume when the audio is being played .