如何在其他应用播放音乐时暂停视频并在音乐停止播放后继续播放?

时间:2019-01-23 11:33:21

标签: android android-mediaplayer android-videoview

正在播放视频时,我从“音乐”应用中的通知中恢复音乐,则视频仍在播放。音乐和视频同时播放。我想要的是在音乐开始时暂停视频。 Youtube应用程序可以执行我在此说明的内容。当谷歌搜索,我发现在这种情况下AudioFocus用于MediaPlayer。如果是,VideoView是否支持AudioFocus,那么如何使用它来实现上述目标?

1 个答案:

答案 0 :(得分:1)

您可以调用changeAudioFocus(true);

当您开始通过应用播放媒体时。它将创建一个OnAudioFocusChangeListener并使用AudioManager进行设置,当您收到AUDIOFOCUS_LOSS或AUDIOFOCUS_LOSS_TRANSIENT消息时,您将获得回调。

您还将获得AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK,用于降低播放器的音量,某些媒体播放器会在收到通知声音时这样做。

/**
  * Used to indicate a transient loss of audio focus where the loser of the audio focus can
     * lower its output volume if it wants to continue playing (also referred to as "ducking"), as
     * the new focus owner doesn't require others to be silent.
     * @see OnAudioFocusChangeListener#onAudioFocusChange(int)
     */

代码

    private final OnAudioFocusChangeListener mAudioFocusListener = createOnAudioFocusChangeListener();
    private AudioManager mAudioManager = null;
    private boolean mHasAudioFocus = false;
    private void changeAudioFocus(boolean acquire) {
        if (mAudioManager == null)
            mAudioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
        if (mAudioManager == null)
            return;

        if (acquire) {
            if (!mHasAudioFocus) {
                final int result = mAudioManager.requestAudioFocus(mAudioFocusListener,
                        AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
                if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
                    mAudioManager.setParameters("bgm_state=true");
                    mHasAudioFocus = true;
                }
            }
        } else if (mHasAudioFocus) {
            mAudioManager.abandonAudioFocus(mAudioFocusListener);
            mAudioManager.setParameters("bgm_state=false");
            mHasAudioFocus = false;
        }
    }

private OnAudioFocusChangeListener createOnAudioFocusChangeListener() {
    return new OnAudioFocusChangeListener() {
        int audioDuckLevel = -1;
        private int mLossTransientVolume = -1;
        private boolean wasPlaying = false;

        @Override
        public void onAudioFocusChange(int focusChange) {
            /*
             * Pause playback during alerts and notifications
             */
            switch (focusChange) {
                case AudioManager.AUDIOFOCUS_LOSS:
                    if (BuildConfig.DEBUG) Log.i(TAG, "AUDIOFOCUS_LOSS");
                    // Pause playback
                    changeAudioFocus(false);
                    pause();
                    break;
                case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
                    if (BuildConfig.DEBUG) Log.i(TAG, "AUDIOFOCUS_LOSS_TRANSIENT");
                    // Pause playback
                    pausePlayback();
                    break;
                case AudioManager.AUDIOFOCUS_GAIN:
                    if (BuildConfig.DEBUG) Log.i(TAG, "AUDIOFOCUS_GAIN: ");
                    // Resume playback
                    if (mLossTransientVolume != -1) {
                            mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, mLossTransientVolume, 0);
                        mLossTransientVolume = -1;
                    }
                    if (mLossTransient) {
                        if (wasPlaying && mSettings.getBoolean("resume_playback", true))
                            play();
                        mLossTransient = false;
                    }
                    break;
            }
        }

        private void pausePlayback() {
            if (mLossTransient) return;
            mLossTransient = true;
            wasPlaying = isPlaying();
            if (wasPlaying) pause();
        }
    };
}