Audio Focus Request Xamarin Android Audio Player

Request other apps to use the audio hardware to reduce the noisy output.

Introduction

Consider we are creating an audio player application. There may be a chance of creating noisy output on the speaker because another app uses the audio hardware at the same time. To reduce this, Android has a built-in API called AudioFocusRequest.
 
Step 1

We have to register for an audio listener, so that the listener class will provide the information whether another application is using the audio or not.
 
Step 2

Add the following code to the class where you're going to implement the listeners. We have to get the audio service using AudioManger.
  1.  public bool RequestAudioFocus()  
  2.         {  
  3.             var amanager = (AudioManager)GetSystemService(AudioService);  
  4.             AudioFocusRequest audioFocusRequest;  
  5.             if (Build.VERSION.SdkInt > BuildVersionCodes.O)  
  6.             {  
  7.                 audioFocusRequest = amanager.RequestAudioFocus(new AudioFocusRequestClass.Builder(AudioFocus.Gain)  
  8.                                            .SetAudioAttributes(new AudioAttributes.Builder().SetLegacyStreamType(Stream.Music).Build())  
  9.                                            .SetOnAudioFocusChangeListener(this)  
  10.                                            .Build());  
  11.             }  
  12.             else  
  13.             {  
  14.                 audioFocusRequest = amanager.RequestAudioFocus(this, Stream.Music, AudioFocus.Gain);  
  15.             }  
  16.   
  17.             if (audioFocusRequest == AudioFocusRequest.Granted)  
  18.             {  
  19.                 return true;  
  20.             }            
  21.     return false;  
  22. }  
Step 3

Implement the interface "AudioManager.IOnAudioFocusChangeListener"  which implements a method called.

OnAudioFocusChange.   
  1. public void OnAudioFocusChange([GeneratedEnum] AudioFocus focusChange)  
  2. {  
  3. }  
Step 4

Inside "OnAudioFocusChange" Method implement the switch case to handle the audio Focus.
  1. switch (focusChange)  
  2.           {  
  3.               case AudioFocus.Gain:  
  4.   
  5.                 //Gain when other Music Player app releases the audio service
  6.                  
  7.                   }  
  8.                   break;  
  9.               case AudioFocus.Loss:  
  10.                   //We have lost focus stop!  
  11.                     
  12.                   break;  
  13.               case AudioFocus.LossTransient:  
  14.                   //We have lost focus for a short time, but likely to resume so pause  
  15.                  
  16.                   break;  
  17.               case AudioFocus.LossTransientCanDuck:  
  18.                   //We have lost focus but should till play at a muted 10% volume  
  19.                   
  20.                   break;  
  21.           } 
Step 5

The final implemetation will be like below.
  1. public class AudioFocus : AudioManager.IOnAudioFocusChangeListener  
  2. {  
  3.     public AudioFocus()  
  4.     {  
  5.         RequestAudioFocus();  
  6.     }  
  7.     public bool RequestAudioFocus()  
  8.     {  
  9.         audioManager = (AudioManager)GetSystemService(AudioService);  
  10.         AudioFocusRequest audioFocusRequest;  
  11.         if (Build.VERSION.SdkInt > BuildVersionCodes.O)  
  12.         {  
  13.             audioFocusRequest = audioManager.RequestAudioFocus(new AudioFocusRequestClass.Builder(AudioFocus.Gain)  
  14.             .SetAudioAttributes(new AudioAttributes.Builder().SetLegacyStreamType(Stream.Music).Build())              .SetOnAudioFocusChangeListener(this)  
  15.             .Build());  
  16.         }  
  17.         else  
  18.         {  
  19.             audioFocusRequest = audioManager.RequestAudioFocus(this, Stream.Music, AudioFocus.Gain);  
  20.         }  
  21.   
  22.         if (audioFocusRequest == AudioFocusRequest.Granted)  
  23.         {  
  24.             return true;  
  25.         }  
  26.         return false;  
  27.     }  
  28.   
  29.     public void OnAudioFocusChange([GeneratedEnum] AudioFocus focusChange)  
  30.     {  
  31.         switch (focusChange)  
  32.         {  
  33.             case AudioFocus.Gain:  
  34.   
  35.                 //Gain when other Music Player app releases the audio service   
  36.                 break;  
  37.             case AudioFocus.Loss:  
  38.                 //We have lost focus stop!   
  39.   
  40.                 break;  
  41.             case AudioFocus.LossTransient:  
  42.                 //We have lost focus for a short time, but likely to resume so pause   
  43.   
  44.                 break;  
  45.             case AudioFocus.LossTransientCanDuck:  
  46.                 //We have lost focus but should till play at a muted 10% volume   
  47.   
  48.                 break;  
  49.         }  
  50.     }  
  51. }  
At last, remember to release the audio service.