Android abandonAudioFocus不会触发AudioManager.AUDIOFOCUS_LOSS

时间:2014-08-30 15:02:03

标签: android android-mediaplayer

我放弃了onStop()方法中的音频焦点,但这不会导致我的OnAudioFocusChangeListener失去焦点。 放弃甚至不会改变焦点。

我的错误是什么?

我如何获得焦点:

   private void startPlaying() {
      int result = am.requestAudioFocus(afChangeListener,
            AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
      if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
         // Start playback.
         mediaplayer.start();
         if (am.isBluetoothA2dpOn()) {
            // Adjust output for Bluetooth.
            Log.i(AudioDemo.class.getName(), "Audio traget: BTHeadset");
         } else if (am.isSpeakerphoneOn()) {
            // Adjust output for Speakerphone.
            Log.i(AudioDemo.class.getName(), "Audio traget: Speakerphone");
         } else if (am.isWiredHeadsetOn()) {
            // Adjust output for headsets
            Log.i(AudioDemo.class.getName(), "Audio traget: WiredHeadset");
         } else {
            // If audio plays and noone can hear it, is it still playing?
            Log.i(AudioDemo.class.getName(), "Audio traget: None");
         }
         isAudioPlaying = true;
         tb = (ToggleButton) findViewById(R.id.toggle_audio);
         tb.setChecked(true);
      }
   }

onStop()

        @Override
       protected void onStop() {
          Log.i(AudioDemo.class.getName(), "onStop()");
          // Abandon audio focus when playback complete, does nothing
          int result = am.abandonAudioFocus(afChangeListener);
          if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
             Log.i(AudioDemo.class.getName(), "Abandonning focus worked.");
          } else {
             Log.i(AudioDemo.class.getName(), "Abandonning focus failed.");
          }
          super.onStop();
       }

我的听众:

   afChangeListener = new OnAudioFocusChangeListener() {
     public void onAudioFocusChange(int focusChange) {
        Log.i(AudioDemo.class.getName(), "focusChange: " + focusChange);
        if ((focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT)
              || (focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK)) {
           if ((mediaplayer != null) && (mediaplayer.isPlaying())) {
              // 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
              mediaplayer.pause();
           }
           isAudioPlaying = false;
        } else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) {
           // Resume playback
           if (mediaplayer == null) {
              mediaplayer = MediaPlayer.create(AudioDemo.this, R.raw.song);
           }
           if (!mediaplayer.isPlaying()) {
              mediaplayer.start();
           }
           isAudioPlaying = true;
           // Lost focus for an unbounded amount of time: stop playback and
           // release media player
        } else if (focusChange == AudioManager.AUDIOFOCUS_LOSS) {
           // Stop listening for button presses
           am.unregisterMediaButtonEventReceiver(audioDemoReceiver);
           am.abandonAudioFocus(afChangeListener);
           if (mediaplayer != null) {
              if (mediaplayer.isPlaying()) {
                 // Stop playback
                 mediaplayer.stop();
              }
              isAudioPlaying = false;
              mediaplayer.release();
              mediaplayer = null;
           }
        }
        tb.setChecked(isAudioPlaying);
     }
  };

问候

0 个答案:

没有答案