O'Reilly logo

Xamarin Mobile Development for Android Cookbook by Matthew Leibowitz

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required