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. if (audioFocusRequest == AudioFocusRequest.Granted)
  17. {
  18. return true;
  19. }
  20. return false;
  21. }
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. //Gain when other Music Player app releases the audio service
  5. }
  6. break;
  7. case AudioFocus.Loss:
  8. //We have lost focus stop!
  9. break;
  10. case AudioFocus.LossTransient:
  11. //We have lost focus for a short time, but likely to resume so pause
  12. break;
  13. case AudioFocus.LossTransientCanDuck:
  14. //We have lost focus but should till play at a muted 10% volume
  15. break;
  16. }
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. if (audioFocusRequest == AudioFocusRequest.Granted)
  22. {
  23. return true;
  24. }
  25. return false;
  26. }
  27. public void OnAudioFocusChange([GeneratedEnum] AudioFocus focusChange)
  28. {
  29. switch (focusChange)
  30. {
  31. case AudioFocus.Gain:
  32. //Gain when other Music Player app releases the audio service
  33. break;
  34. case AudioFocus.Loss:
  35. //We have lost focus stop!
  36. break;
  37. case AudioFocus.LossTransient:
  38. //We have lost focus for a short time, but likely to resume so pause
  39. break;
  40. case AudioFocus.LossTransientCanDuck:
  41. //We have lost focus but should till play at a muted 10% volume
  42. break;
  43. }
  44. }
  45. }
At last, remember to release the audio service.