From 4b3a4aa47a142483fe0fafeac8cefd2378821dfc Mon Sep 17 00:00:00 2001 From: maiwenchang Date: Fri, 5 Jun 2020 09:04:21 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E9=9F=B3=E9=A2=91=E7=84=A6?= =?UTF-8?q?=E7=82=B9=E7=9B=91=E5=90=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../artplayer/audio/DefaultAudioFocusChangeListener.kt | 5 ++++- .../java/org/salient/artplayer/audio/DefaultAudioManager.kt | 3 ++- .../src/main/java/org/salient/artplayer/ui/VideoView.kt | 5 ----- 3 files changed, 6 insertions(+), 7 deletions(-) diff --git a/artplayer-core/src/main/java/org/salient/artplayer/audio/DefaultAudioFocusChangeListener.kt b/artplayer-core/src/main/java/org/salient/artplayer/audio/DefaultAudioFocusChangeListener.kt index 90ddfb6..ac52f50 100644 --- a/artplayer-core/src/main/java/org/salient/artplayer/audio/DefaultAudioFocusChangeListener.kt +++ b/artplayer-core/src/main/java/org/salient/artplayer/audio/DefaultAudioFocusChangeListener.kt @@ -5,6 +5,7 @@ import android.content.Context import android.media.AudioManager import android.media.AudioManager.OnAudioFocusChangeListener import org.salient.artplayer.player.IMediaPlayer +import java.lang.ref.WeakReference /** * description: 声音焦点变化管理类 @@ -14,7 +15,7 @@ import org.salient.artplayer.player.IMediaPlayer * date: 2020-05-04 10:06 AM. */ open class DefaultAudioFocusChangeListener( - private val context: Context, + private val contextReference: WeakReference, private val audioManager: IAudioManager, private val mediaPlayer: IMediaPlayer<*>? ) : OnAudioFocusChangeListener { @@ -22,6 +23,7 @@ open class DefaultAudioFocusChangeListener( override fun onAudioFocusChange(focusChange: Int) { when (focusChange) { AudioManager.AUDIOFOCUS_GAIN -> { + val context = contextReference.get() ?: return // 重新获得焦点,恢复正常音量,恢复播放 if (playOnAudioFocus && mediaPlayer?.isPlaying != true) { mediaPlayer?.start(); @@ -32,6 +34,7 @@ open class DefaultAudioFocusChangeListener( } AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK -> { //短暂失去焦点,无须停止播放,只适当降低播放器音量 + val context = contextReference.get() ?: return val duckVolume = getCurrentVolume(context) * 0.8f mediaPlayer?.setVolume(duckVolume); } diff --git a/artplayer-core/src/main/java/org/salient/artplayer/audio/DefaultAudioManager.kt b/artplayer-core/src/main/java/org/salient/artplayer/audio/DefaultAudioManager.kt index 89bea3e..8c5f048 100644 --- a/artplayer-core/src/main/java/org/salient/artplayer/audio/DefaultAudioManager.kt +++ b/artplayer-core/src/main/java/org/salient/artplayer/audio/DefaultAudioManager.kt @@ -7,6 +7,7 @@ import android.media.AudioFocusRequest import android.media.AudioManager import android.os.Build import org.salient.artplayer.player.IMediaPlayer +import java.lang.ref.WeakReference /** * description: 音频管理 @@ -24,7 +25,7 @@ open class DefaultAudioManager(context: Context, mediaPlayer: IMediaPlayer<*>?) .build() private var audioFocusRequest: AudioFocusRequest? = null - override var onAudioFocusChangeListener: AudioManager.OnAudioFocusChangeListener = DefaultAudioFocusChangeListener(context, this, mediaPlayer) + override var onAudioFocusChangeListener: AudioManager.OnAudioFocusChangeListener = DefaultAudioFocusChangeListener(WeakReference(context), this, mediaPlayer) override fun mute() { audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 0, AudioManager.FLAG_PLAY_SOUND) diff --git a/artplayer-core/src/main/java/org/salient/artplayer/ui/VideoView.kt b/artplayer-core/src/main/java/org/salient/artplayer/ui/VideoView.kt index e2587cc..029842d 100644 --- a/artplayer-core/src/main/java/org/salient/artplayer/ui/VideoView.kt +++ b/artplayer-core/src/main/java/org/salient/artplayer/ui/VideoView.kt @@ -320,9 +320,4 @@ open class VideoView : FrameLayout, IVideoView { } } - override fun onDetachedFromWindow() { - removeMediaPlayerObserver(mediaPlayer) - super.onDetachedFromWindow() - } - } \ No newline at end of file