, , , ,

AudioManager class in Android provides access to volume and ringer mode control. Since Xamarin has 100% Android coverage, we can use the same class to control system wide volume and ringer via our C# code. We have to invoke Context.getSystemService(Context.AUDIO_SERVICE) to make an instance of the AudioManager class and after getting an instance, we can call it and invoke the methods to control our Audio peripherals in the device.

A very simple code snippet is

AudioManager am = (AudioManager)this.GetSystemService (Context.AudioService);
am.RingerMode = RingerMode.Vibrate;

and never forget to use

using Android.Media;

if you see in the first two lines of code, in the first line we have used this as the Context to instantiate the AudioManager class. In the second line, we programtically set device’s audio mode to Vibrate.

The same code for Android is something like,

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

To adjust (say for instance, increase) the ringer volume,

am.AdjustSuggestedStreamVolume (Adjust.Raise, Android.Media.Stream.Ring, VolumeNotificationFlags.ShowUi);

we can use this in Xamarin. Here, the first parameter, Adjust.Raise defines, the volume is increasing. The Adjust enum has 3 items, they are Lower, Raise and Same. The second parameter, Android.Media.Stream.Ring defines, it is ringer stream and it has 8 items, they are Alarm, Dtmf, Music, Notification, NotificationDefault, Ring, System and VoiceCall. VolumeNotificationFlags provides some more UX control over the action and it also has 5 enum items.