AudioManager in Xamarin.Android

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.

2 thoughts on “AudioManager in Xamarin.Android

  1. Hi Isham, I am trying to develop a small app which will sets the ringer volume to highest when the mobile recieves a message as “lostphone”. OnReceive event is fired whenever a new message is recieved to my mobile number. Now I want to modify the ringer and unable to progress.

    I am getting below error:

    ‘SMSBroadcastReceiver’ does not contain a definition for ‘GetSystemService’ and no extension method ‘GetSystemService’ accepting a first argument of type ‘SMSBroadcastReceiver’ could be found (are you missing a using directive or an assembly reference?)

    Below is my class definition:

    [BroadcastReceiver(Enabled = true)]
    [IntentFilter(new[] { “android.provider.Telephony.SMS_RECEIVED” }, Priority = (int)IntentFilterPriority.HighPriority)]

    public class SMSBroadcastReceiver : BroadcastReceiver

    1. Hi,

      There seems some issues with SMS communication.
      You can try couple of things
      1) Make sure you have added related permissions to the manifest.
      2) Disable SMS in hangouts

      Let me know how it goes. I will share an SMS sender application sample for Xamarin later this week.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s