Managing the audio volume

When we play music, for example in a media player or game, we need to be able to control the volume based on what the user is doing. For example, if the user receives a call, the music should be paused. If there is an alarm, the volume should drop.

How to do it...

We can respond to various system requests to reduce the volume of media. One instance of this is when the headphones are removed, we may wish to pause the playback so as to avoid unexpected loud sounds:

  1. If we want to pause music when the headphones are removed, we can respond to the ActionAudioBecomingNoisy instance broadcast and pause the playback:
    [BroadcastReceiver] [IntentFilter( new[]{ AudioManager.ActionAudioBecomingNoisy })] public class MediaReceiver : BroadcastReceiver ...

Get Xamarin Mobile Development for Android Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.