title: How to determine that other app lose the audio focus so that i can start my player
body: <p>I have seen many Android Player online that as soon it start playing other app loses the focus and stop playing. </p>
  
  <p>At other hand, as soon the gained focused app stop playing, focus Loosed app start playing again.</p>
  
  <p>can any one suggest what am i missing here to achieve the same in my app? I want as soon other app stop playing my app should GAIN focus and start playing..</p>
  
  <pre><code>private void setupAudioManager() {
          audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
          audioManager.requestAudioFocus(audioFocusListener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
          audioFocusListener = new OnAudioFocusChangeListener() {
              @Override
              public void onAudioFocusChange(int focusChange) {
                  switch (focusChange) {
                      case AUDIOFOCUS_GAIN:
                          if (mediaPlayer == null) setupMediaPlayer();
                          else if (!mediaPlayer.isPlaying()) {
                              play();
                          }
                          mediaPlayer.setVolume(MEDIA_PLAYER_LEFT_VOLUME, MEDIA_PLAYER_RIGHT_VOLUME);
                          break;
  
                      case AUDIOFOCUS_LOSS:
                          if (isPlaying()) {
                              Intent intent = new Intent("HomeActivity");
                              intent.putExtra("playerState", "pause");
                              LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(intent);
                          }
  
                          break;
  
                      case AUDIOFOCUS_LOSS_TRANSIENT:
                          if (isPlaying()) pause();
                          break;
  
                      case AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
                          if (isPlaying()) mediaPlayer.setVolume(MEDIA_PLAYER_LEFT_VOLUME_LOW,
                                  MEDIA_PLAYER_RIGHT_VOLUME_LOW);
                          break;
  
                  }
              }
          };
      }
  </code></pre>
  
  <p>Looking for you suggestion.</p>