From e793e9c5e69abb53e1fb8dabcca844fc869a256f Mon Sep 17 00:00:00 2001 From: ashiagr Date: Fri, 28 Mar 2025 18:30:14 +0530 Subject: [PATCH] Add delay before connecting surface --- .../player/view/video/VideoView.kt | 31 +++++++++++++++++-- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/modules/features/player/src/main/java/au/com/shiftyjelly/pocketcasts/player/view/video/VideoView.kt b/modules/features/player/src/main/java/au/com/shiftyjelly/pocketcasts/player/view/video/VideoView.kt index e6903eae56e..7f5248ece87 100644 --- a/modules/features/player/src/main/java/au/com/shiftyjelly/pocketcasts/player/view/video/VideoView.kt +++ b/modules/features/player/src/main/java/au/com/shiftyjelly/pocketcasts/player/view/video/VideoView.kt @@ -11,14 +11,19 @@ import androidx.media3.ui.AspectRatioFrameLayout import au.com.shiftyjelly.pocketcasts.player.R import au.com.shiftyjelly.pocketcasts.repositories.playback.PlaybackManager import au.com.shiftyjelly.pocketcasts.repositories.playback.SimplePlayer +import au.com.shiftyjelly.pocketcasts.utils.log.LogBuffer class VideoView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) : FrameLayout(context, attrs, defStyleAttr), SurfaceHolder.Callback, SimplePlayer.VideoChangedListener { + companion object { + private const val DELAY_MS = 300L + private const val TAG = "VideoView" + } var playbackManager: PlaybackManager? = null var show: Boolean = false set(value) { field = value - connect() + connectWithDelay() } private val view = LayoutInflater.from(context).inflate(R.layout.video_view, this, true) @@ -30,6 +35,7 @@ class VideoView @JvmOverloads constructor(context: Context, attrs: AttributeSet? private val surfaceView = view.findViewById(R.id.surfaceView) private var isSurfaceCreated: Boolean = false private var isSurfaceSet: Boolean = false + private var pendingConnection = false init { surfaceView.holder.addCallback(this) @@ -78,7 +84,7 @@ class VideoView @JvmOverloads constructor(context: Context, attrs: AttributeSet? override fun surfaceCreated(holder: SurfaceHolder) { isSurfaceCreated = true isSurfaceSet = false - connect() + connectWithDelay() } override fun surfaceChanged(holder: SurfaceHolder, format: Int, width: Int, height: Int) { @@ -87,11 +93,30 @@ class VideoView @JvmOverloads constructor(context: Context, attrs: AttributeSet? override fun surfaceDestroyed(holder: SurfaceHolder) { isSurfaceCreated = false isSurfaceSet = false + pendingConnection = false // Cancel any pending connection } fun updatePlayerPrepared(prepared: Boolean) { if (prepared && !isSurfaceSet) { - connect() + connectWithDelay() } } + + private fun connectWithDelay() { + pendingConnection = true + // Temporary fix for https://github.com/Automattic/pocket-casts-android/issues/3807 + // The delay gives time for the Activity/Fragment transition to complete and + // prevents the race condition where surface gets destroyed immediately after creation + + postDelayed({ + if (pendingConnection) { + try { + connect() + } catch (e: Exception) { + LogBuffer.e(TAG, "Failed to connect video surface", e) + } + } + pendingConnection = false + }, DELAY_MS) + } }