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:
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 { public override void OnReceive( Context context, Intent intent) { context.StartService(new Intent( MediaService.ActionPause, null, context, typeof(MediaService))); } }
Also, we can respond to requests from other...