Skip to content

Commit 7c274ca

Browse files
committed
Update PlayerSurface to directly use AndroidView
The proxy classes Android(Embedded)ExternalSurface just provide a simple API surface around AndroidView wrapping SurfaceView and TextureView respectively. However, this prevents accessing the underlying views directly, which is needed for full lifecycle tracking by the Player and to access surface size updates (which are not available when the API is reduced to just `Surface`). Instead of the proxy classes, we can directly use AndroidView from PlayerSurface. This allows to call the proper Player APIs to set SurfaceView or TextureView, so that the Player can keep track of the view lifecycle and update its internal state and size tracking accordingly. Because the player keeps tracks of the lifecycle, none of the callback structure in Android(Embedded)ExternalSurface is needed, nor are the additional setters for options that are all default. PiperOrigin-RevId: 743079058 (cherry picked from commit a1ed0d4)
1 parent 9d09840 commit 7c274ca

File tree

4 files changed

+135
-26
lines changed

4 files changed

+135
-26
lines changed

RELEASENOTES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@
4747
* IMA extension:
4848
* Session:
4949
* UI:
50+
* Enable `PlayerSurface` to work with `ExoPlayer.setVideoEffects` and
51+
`CompositionPlayer`.
5052
* Downloads:
5153
* Add partial download support for progressive streams. Apps can prepare a
5254
progressive stream with `DownloadHelper`, and request a

libraries/ui_compose/src/main/java/androidx/media3/ui/compose/PlayerSurface.kt

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,22 @@
1616

1717
package androidx.media3.ui.compose
1818

19-
import android.view.Surface
19+
import android.view.SurfaceView
20+
import android.view.TextureView
2021
import androidx.annotation.IntDef
21-
import androidx.compose.foundation.AndroidEmbeddedExternalSurface
22-
import androidx.compose.foundation.AndroidExternalSurface
23-
import androidx.compose.foundation.AndroidExternalSurfaceScope
2422
import androidx.compose.runtime.Composable
2523
import androidx.compose.runtime.getValue
2624
import androidx.compose.runtime.rememberUpdatedState
2725
import androidx.compose.ui.Modifier
26+
import androidx.compose.ui.viewinterop.AndroidView
2827
import androidx.media3.common.Player
2928
import androidx.media3.common.util.UnstableApi
3029

3130
/**
3231
* Provides a dedicated drawing [Surface] for media playbacks using a [Player].
3332
*
34-
* The player's video output is displayed with either a
35-
* [android.view.SurfaceView]/[AndroidExternalSurface] or a
36-
* [android.view.TextureView]/[AndroidEmbeddedExternalSurface].
33+
* The player's video output is displayed with either a [android.view.SurfaceView] or a
34+
* [android.view.TextureView].
3735
*
3836
* [Player] takes care of attaching the rendered output to the [Surface] and clearing it, when it is
3937
* destroyed.
@@ -52,32 +50,36 @@ fun PlayerSurface(
5250
// Player might change between compositions,
5351
// we need long-lived surface-related lambdas to always use the latest value
5452
val currentPlayer by rememberUpdatedState(player)
55-
val onSurfaceCreated: (Surface) -> Unit = { surface ->
56-
if (currentPlayer.isCommandAvailable(Player.COMMAND_SET_VIDEO_SURFACE))
57-
currentPlayer.setVideoSurface(surface)
58-
}
59-
val onSurfaceDestroyed: () -> Unit = {
60-
if (currentPlayer.isCommandAvailable(Player.COMMAND_SET_VIDEO_SURFACE))
61-
currentPlayer.clearVideoSurface()
62-
}
63-
val onSurfaceInitialized: AndroidExternalSurfaceScope.() -> Unit = {
64-
onSurface { surface, _, _ ->
65-
onSurfaceCreated(surface)
66-
surface.onDestroyed { onSurfaceDestroyed() }
67-
}
68-
}
6953

7054
when (surfaceType) {
7155
SURFACE_TYPE_SURFACE_VIEW ->
72-
AndroidExternalSurface(modifier = modifier, onInit = onSurfaceInitialized)
56+
AndroidView(
57+
factory = {
58+
SurfaceView(it).apply {
59+
if (currentPlayer.isCommandAvailable(Player.COMMAND_SET_VIDEO_SURFACE))
60+
currentPlayer.setVideoSurfaceView(this)
61+
}
62+
},
63+
onReset = {},
64+
modifier = modifier,
65+
)
7366
SURFACE_TYPE_TEXTURE_VIEW ->
74-
AndroidEmbeddedExternalSurface(modifier = modifier, onInit = onSurfaceInitialized)
67+
AndroidView(
68+
factory = {
69+
TextureView(it).apply {
70+
if (currentPlayer.isCommandAvailable(Player.COMMAND_SET_VIDEO_SURFACE))
71+
currentPlayer.setVideoTextureView(this)
72+
}
73+
},
74+
onReset = {},
75+
modifier = modifier,
76+
)
7577
else -> throw IllegalArgumentException("Unrecognized surface type: $surfaceType")
7678
}
7779
}
7880

7981
/**
80-
* The type of surface view used for media playbacks. One of [SURFACE_TYPE_SURFACE_VIEW] or
82+
* The type of surface used for media playbacks. One of [SURFACE_TYPE_SURFACE_VIEW] or
8183
* [SURFACE_TYPE_TEXTURE_VIEW].
8284
*/
8385
@UnstableApi
@@ -86,7 +88,7 @@ fun PlayerSurface(
8688
@IntDef(SURFACE_TYPE_SURFACE_VIEW, SURFACE_TYPE_TEXTURE_VIEW)
8789
annotation class SurfaceType
8890

89-
/** Surface type equivalent to [android.view.SurfaceView]. */
91+
/** Surface type to create [android.view.SurfaceView]. */
9092
@UnstableApi const val SURFACE_TYPE_SURFACE_VIEW = 1
91-
/** Surface type equivalent to [android.view.TextureView]. */
93+
/** Surface type to create [android.view.TextureView]. */
9294
@UnstableApi const val SURFACE_TYPE_TEXTURE_VIEW = 2
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
* Copyright 2025 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package androidx.media3.ui.compose
17+
18+
import android.view.SurfaceView
19+
import android.view.TextureView
20+
import androidx.compose.runtime.MutableIntState
21+
import androidx.compose.runtime.mutableIntStateOf
22+
import androidx.compose.runtime.remember
23+
import androidx.compose.ui.test.junit4.createComposeRule
24+
import androidx.media3.common.Player
25+
import androidx.media3.ui.compose.utils.TestPlayer
26+
import androidx.test.ext.junit.runners.AndroidJUnit4
27+
import com.google.common.truth.Truth.assertThat
28+
import org.junit.Rule
29+
import org.junit.Test
30+
import org.junit.runner.RunWith
31+
32+
/** Unit test for [PlayerSurface]. */
33+
@RunWith(AndroidJUnit4::class)
34+
class PlayerSurfaceTest {
35+
36+
@get:Rule val composeTestRule = createComposeRule()
37+
38+
@Test
39+
fun playerSurface_withSurfaceViewType_setsSurfaceViewOnPlayer() {
40+
val player = TestPlayer()
41+
42+
composeTestRule.setContent {
43+
PlayerSurface(player = player, surfaceType = SURFACE_TYPE_SURFACE_VIEW)
44+
}
45+
composeTestRule.waitForIdle()
46+
47+
assertThat(player.videoOutput).isInstanceOf(SurfaceView::class.java)
48+
}
49+
50+
@Test
51+
fun playerSurface_withTextureViewType_setsTextureViewOnPlayer() {
52+
val player = TestPlayer()
53+
54+
composeTestRule.setContent {
55+
PlayerSurface(player = player, surfaceType = SURFACE_TYPE_TEXTURE_VIEW)
56+
}
57+
composeTestRule.waitForIdle()
58+
59+
assertThat(player.videoOutput).isInstanceOf(TextureView::class.java)
60+
}
61+
62+
@Test
63+
fun playerSurface_withoutSupportedCommand_doesNotSetSurfaceOnPlayer() {
64+
val player = TestPlayer()
65+
player.removeCommands(Player.COMMAND_SET_VIDEO_SURFACE)
66+
67+
composeTestRule.setContent {
68+
PlayerSurface(player = player, surfaceType = SURFACE_TYPE_TEXTURE_VIEW)
69+
}
70+
composeTestRule.waitForIdle()
71+
72+
assertThat(player.videoOutput).isNull()
73+
}
74+
75+
@Test
76+
fun playerSurface_withUpdateSurfaceType_setsNewSurfaceOnPlayer() {
77+
val player = TestPlayer()
78+
79+
lateinit var surfaceType: MutableIntState
80+
composeTestRule.setContent {
81+
surfaceType = remember { mutableIntStateOf(SURFACE_TYPE_TEXTURE_VIEW) }
82+
PlayerSurface(player = player, surfaceType = surfaceType.intValue)
83+
}
84+
composeTestRule.waitForIdle()
85+
surfaceType.intValue = SURFACE_TYPE_SURFACE_VIEW
86+
composeTestRule.waitForIdle()
87+
88+
assertThat(player.videoOutput).isInstanceOf(SurfaceView::class.java)
89+
}
90+
}

libraries/ui_compose/src/test/java/androidx/media3/ui/compose/utils/TestPlayer.kt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ internal class TestPlayer : SimpleBasePlayer(Looper.myLooper()!!) {
4141
.setPlayWhenReady(true, PLAY_WHEN_READY_CHANGE_REASON_USER_REQUEST)
4242
.build()
4343

44+
var videoOutput: Any? = null
45+
private set
46+
4447
override fun getState(): State {
4548
return state
4649
}
@@ -99,6 +102,18 @@ internal class TestPlayer : SimpleBasePlayer(Looper.myLooper()!!) {
99102
return Futures.immediateVoidFuture()
100103
}
101104

105+
override fun handleSetVideoOutput(videoOutput: Any): ListenableFuture<*> {
106+
this.videoOutput = videoOutput
107+
return Futures.immediateVoidFuture()
108+
}
109+
110+
override fun handleClearVideoOutput(videoOutput: Any?): ListenableFuture<*> {
111+
if (videoOutput == null || videoOutput == this.videoOutput) {
112+
this.videoOutput = null
113+
}
114+
return Futures.immediateVoidFuture()
115+
}
116+
102117
fun setPlaybackState(playbackState: @Player.State Int) {
103118
state = state.buildUpon().setPlaybackState(playbackState).build()
104119
invalidateState()

0 commit comments

Comments
 (0)