title: onAudioFocusChange not getting called
body: <p>I am creating a application where a song is played by streaming. My app will continue  to play  when move to background which is OK. But when some other application like a music player of device or may be when I am playing video from youtube at that time both the media are overlapping and its not going into onAudioFocusChange() 
  Code for the same:</p>
  
  <pre><code>public class sampleMediaPlayer extends Activity implements AudioManager.OnAudioFocusChangeListener, OnClickListener {
  private MediaPlayer mediaPlayer;
  private String myURL; 
  AudioManager audioManager;
  Button buttonStart, buttonStop;
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          // TODO Auto-generated method stub
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
  
          buttonStart = (Button) findViewById(R.id.buttonStart);
          buttonStop = (Button) findViewById(R.id.buttonStop);
  
          buttonStart.setOnClickListener(this);
          buttonStop.setOnClickListener(this);
          audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
  
  
      }
  
  
  @Override
  protected void onResume() {
      // TODO Auto-generated method stub
      super.onResume();
  
  
          Log.e("123", "Inside sampleMediaPlayer");
  
  
  
  }
      @Override
          protected void onStop() {
              // TODO Auto-generated method stub
              super.onStop();
  
          }
  
  
  
      @Override
      public void onAudioFocusChange(int focusChange) {
          // TODO Auto-generated method stub
  
          Log.e("123", "focusChange============&gt;&gt;&gt;&gt;"+focusChange);
           switch (focusChange) {
              case AudioManager.AUDIOFOCUS_GAIN:
                  Log.e("123", "focusChange======AUDIOFOCUS_GAIN======&gt;&gt;&gt;&gt;"+focusChange);
                  requestFocus();
                  // resume playback
                  //AUDIOFOCUS_GAIN (1) : audio focus, or a request of audio focus, of unknown duration.
                  if (mediaPlayer == null)
                      initMediaPlayer();
                  else if (!mediaPlayer.isPlaying()) 
                      mediaPlayer.start();
                  mediaPlayer.setVolume(1.0f, 1.0f);
  
                  break;
  
              case AudioManager.AUDIOFOCUS_GAIN_TRANSIENT:
                  //AUDIOFOCUS_GAIN_TRANSIENT (2) : temporary gain or request of audio focus, e.g. event notification
                  Log.e("123", "focusChange=======AUDIOFOCUS_GAIN_TRANSIENT=====&gt;&gt;&gt;&gt;"+focusChange);
  
  //                  requestFocus();
  //                  if (!mediaPlayer.isPlaying()) 
  //                      mediaPlayer.start();
  
                  break;
  
              case AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK:
                  //AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK (3) : temporary request of audio focus, anticipated to last a short amount of time,
                  //and where it is acceptable for other audio applications to keep playing after having lowered their output level, 
                  //e.g. playback of driving directions where playback of music in the background is acceptable.
                  Log.e("123", "focusChange=======AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK=====&gt;&gt;&gt;&gt;"+focusChange);
  //                  requestFocus();
                  break;
  
              case AudioManager.AUDIOFOCUS_LOSS:
                  // Lost focus for an unbounded amount of time: stop playback and release media player
                  // -1
                  Log.e("123", "focusChange=====AUDIOFOCUS_LOSS=======&gt;&gt;&gt;&gt;"+focusChange);
                  if (mediaPlayer.isPlaying())
                      mediaPlayer.stop();
                  mediaPlayer.release();
                  mediaPlayer = null;
                  abandonFocus();
                  break;
  
              case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
                  Log.e("123", "focusChange======AUDIOFOCUS_LOSS_TRANSIENT======&gt;&gt;&gt;&gt;"+focusChange);
                  // Lost focus for a short time, but we have to stop
                  // playback. We don't release the media player because playback
                  // is likely to resume
                  //-2
                  if (mediaPlayer.isPlaying()) 
                      mediaPlayer.pause();
                  abandonFocus();
                  break;
  
              case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
                  Log.e("123", "focusChange======AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK======&gt;&gt;&gt;&gt;"+focusChange);
                  // Lost focus for a short time, but it's ok to keep playing
                  // at an attenuated level
                  //-3
                  if (mediaPlayer.isPlaying()) 
                      mediaPlayer.setVolume(0.1f, 0.1f);
                  break;
          }
      }
  
  
  
      @SuppressLint("NewApi")
      private void initMediaPlayer() {
          // TODO Auto-generated method stub
          if(mediaPlayer==null)
              mediaPlayer =  new MediaPlayer();
          //mediaPlayer =  MediaPlayer.create(this, R.raw.braincandy);
  
          try {
              if(!mediaPlayer.isPlaying()){
                  mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
                  myURL = "http://202.191.185.2:10406/thathi_thathi.mp3";
                  mediaPlayer.setDataSource(myURL);
                  mediaPlayer.prepare();
              }
  
  
  
  
              int result = audioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
              boolean flag    =   requestFocus();
  
              Log.e("123", "result===============&gt;&gt;&gt;"+result);
              if (result != AudioManager.AUDIOFOCUS_REQUEST_GRANTED &amp;&amp; flag==true) {
                  // could not get audio focus.
                  Toast.makeText(this, "AudioFocus Request Failed", Toast.LENGTH_SHORT).show();
              }else if(result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED){
                  Toast.makeText(this, "AudioFocus Request Granted", Toast.LENGTH_SHORT).show();
              }
  
  
          } catch (IllegalArgumentException e) {
              // TODO Auto-generated catch block
              Log.e("123", "Illegal Argument Exception");
              e.printStackTrace();
          } catch (SecurityException e) {
              // TODO Auto-generated catch block
              Log.e("123", "Security Ecxeption");
              e.printStackTrace();
          //}
          } catch (IOException e) {
              // TODO Auto-generated catch block
              Log.e("123", "IO Exception----------");
              e.printStackTrace();
          }
  
      //  setAudioManager();
  
          mediaPlayer.start();
          Log.e("123", "Media Player started");
      }
  
  
      @Override
      public void onClick(View arg0) {
          // TODO Auto-generated method stub
  
          switch (arg0.getId()) {
          case R.id.buttonStart:
  
            Log.d("123", "onClick: starting srvice");
  //            startService(new Intent(this, MyService.class));
          //  afh.requestFocus();
            requestFocus();
            initMediaPlayer();
  
  
            break;
          case R.id.buttonStop:
            Log.d("123", "onClick: stopping srvice");
  //            stopService(new Intent(this, MyService.class));
            //afh.abandonFocus();
            if(mediaPlayer != null){
            mediaPlayer.stop();
            mediaPlayer.release();
            abandonFocus();
            }
            break;
          }
  
      }
  
      public boolean requestFocus() {
          return AudioManager.AUDIOFOCUS_REQUEST_GRANTED ==
                  audioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC,
              AudioManager.AUDIOFOCUS_GAIN);
      }
  
      public boolean abandonFocus() {
          return AudioManager.AUDIOFOCUS_REQUEST_GRANTED ==
                  audioManager.abandonAudioFocus(this);
      }
  }
  </code></pre>