Skip to content

Commit 77dcf73

Browse files
committed
Temporary fix for video not playing in landscape mode (#3823)
(cherry picked from commit f20d127)
1 parent 2572bee commit 77dcf73

File tree

1 file changed

+28
-3
lines changed
  • modules/features/player/src/main/java/au/com/shiftyjelly/pocketcasts/player/view/video

1 file changed

+28
-3
lines changed

modules/features/player/src/main/java/au/com/shiftyjelly/pocketcasts/player/view/video/VideoView.kt

+28-3
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,19 @@ import androidx.media3.ui.AspectRatioFrameLayout
1111
import au.com.shiftyjelly.pocketcasts.player.R
1212
import au.com.shiftyjelly.pocketcasts.repositories.playback.PlaybackManager
1313
import au.com.shiftyjelly.pocketcasts.repositories.playback.SimplePlayer
14+
import au.com.shiftyjelly.pocketcasts.utils.log.LogBuffer
1415

1516
class VideoView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) : FrameLayout(context, attrs, defStyleAttr), SurfaceHolder.Callback, SimplePlayer.VideoChangedListener {
17+
companion object {
18+
private const val DELAY_MS = 300L
19+
private const val TAG = "VideoView"
20+
}
1621

1722
var playbackManager: PlaybackManager? = null
1823
var show: Boolean = false
1924
set(value) {
2025
field = value
21-
connect()
26+
connectWithDelay()
2227
}
2328

2429
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?
3035
private val surfaceView = view.findViewById<SurfaceView>(R.id.surfaceView)
3136
private var isSurfaceCreated: Boolean = false
3237
private var isSurfaceSet: Boolean = false
38+
private var pendingConnection = false
3339

3440
init {
3541
surfaceView.holder.addCallback(this)
@@ -78,7 +84,7 @@ class VideoView @JvmOverloads constructor(context: Context, attrs: AttributeSet?
7884
override fun surfaceCreated(holder: SurfaceHolder) {
7985
isSurfaceCreated = true
8086
isSurfaceSet = false
81-
connect()
87+
connectWithDelay()
8288
}
8389

8490
override fun surfaceChanged(holder: SurfaceHolder, format: Int, width: Int, height: Int) {
@@ -87,11 +93,30 @@ class VideoView @JvmOverloads constructor(context: Context, attrs: AttributeSet?
8793
override fun surfaceDestroyed(holder: SurfaceHolder) {
8894
isSurfaceCreated = false
8995
isSurfaceSet = false
96+
pendingConnection = false // Cancel any pending connection
9097
}
9198

9299
fun updatePlayerPrepared(prepared: Boolean) {
93100
if (prepared && !isSurfaceSet) {
94-
connect()
101+
connectWithDelay()
95102
}
96103
}
104+
105+
private fun connectWithDelay() {
106+
pendingConnection = true
107+
// Temporary fix for https://github.com/Automattic/pocket-casts-android/issues/3807
108+
// The delay gives time for the Activity/Fragment transition to complete and
109+
// prevents the race condition where surface gets destroyed immediately after creation
110+
111+
postDelayed({
112+
if (pendingConnection) {
113+
try {
114+
connect()
115+
} catch (e: Exception) {
116+
LogBuffer.e(TAG, "Failed to connect video surface", e)
117+
}
118+
}
119+
pendingConnection = false
120+
}, DELAY_MS)
121+
}
97122
}

0 commit comments

Comments
 (0)