title: Losing Audiofocus
body: <p>i wrote a podcast audio player and included the audio focus principle.</p>
  
  <p>This is my method, which is called in the onPrepared() method of the media player:</p>
  
  <pre><code>private boolean requestAudioFocus(){
  
      AudioManager am = (AudioManager) getSystemService(AUDIO_SERVICE);
  
      OnAudioFocusChangeListener afChangeListener = new OnAudioFocusChangeListener() {
          public void onAudioFocusChange(int focusChange) {
              if (focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT){
                  pause();
              } else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) {
                  play();
              } else if (focusChange == AudioManager.AUDIOFOCUS_LOSS) {
                  Log.d("MYAUDIOSERVICE","Audio Focus lost");
                  finish();
              }
          }
      };
  
      // Request audio focus for playback
      int result = am.requestAudioFocus(afChangeListener,
                                       // Use the music stream.
                                       AudioManager.STREAM_MUSIC,
                                       // Request permanent focus.
                                       AudioManager.AUDIOFOCUS_GAIN);
  
      if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
          return true;
      }
      else{
          return false;
      }
  }
  </code></pre>
  
  <p>The problem is:</p>
  
  <p>Everything works finde when starting the app and playing the first podcast. Audiofocus is gained and the podcast keeps playing.</p>
  
  <p>When i stop playback and start another podcast the audiofocus is gained again, but shortly after AudioManager.AUDIOFOCUS_LOSS is receiver by the onAudioFocusChangeListener.</p>
  
  <p>I guess thats the case because my app has the focus, request it again, gains it but at the same time loses the "old" one. </p>
  
  <p>Am i right, and if if thats correct, can i just give up the audio focus when ending playback on the old podcast ?</p>
  
  <p>Thx in advance</p>