Reputation: 891
In Android 4.0+ there is an option Settings->Accessibility-> Turn of all sounds. If I check that option, any application running on the android device will not produce any sound at all. My application has to give alarm sound, so if someone has checked that option, the app does not give any sound. So, I have to automatically un-check that option every time user launches the application. Through code, how can I do that?
It will be helpful if someone can share a piece of code. I have tried AudioManager, but that only works if mobile audio is enabled.
amanger.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
amanger.setStreamMute(AudioManager.STREAM_MUSIC, false);
amanger.setStreamVolume(AudioManager.STREAM_MUSIC,
(int)(amanger.getStreamMaxVolume(AudioManager.STREAM_MUSIC)*(75.0/100.0)), 0);
Upvotes: 9
Views: 20526
Reputation: 1400
You can use following function to disable system volume
public static void disableSound(Context context)
{
AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
audioManager.setStreamVolume(AudioManager.STREAM_SYSTEM, 0, 0);
}
If you want to enable it again, just change the parameters
audioManager.setStreamVolume(AudioManager.STREAM_SYSTEM, 10, 0);
Volume ranges from 0-10
Upvotes: 2