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
and never forget to use
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,
To adjust (say for instance, increase) the ringer volume,
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
Same. The second parameter,
Android.Media.Stream.Ring defines, it is ringer stream and it has 8 items, they are
VolumeNotificationFlags provides some more UX control over the action and it also has 5 enum items.