Sanik Sanik - 4 months ago 15
Android Question

Android:How can I unmute the user's phone programmatically?

I have created an app where on button click music plays and on back press, music stops. What if the user's phone is in silent mode or muted? How can I automatically turn on user's speaker while playing these music files programmatically or how can I give an option to the user to unmute or keep it in mute???

Answer

You can use the AudioManager to unmute the phone:

Context context = getActivity();
AudioManager audioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_RING);

audioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
audioManager.setStreamVolume(AudioManager.STREAM_RING, maxVolume, AudioManager.FLAG_SHOW_UI + AudioManager.FLAG_PLAY_SOUND);