This page describes the different types of surfaces that can be used for video playback with Media3, and how to choose the right type for your use case. To find out more about Surface objects in Android, read this graphics documentation.
Set the surface
There are four entry points for the Player to connect its video output to some
Surface:
void setVideoSurface(@Nullable Surface surface)void setVideoSurfaceHolder(@Nullable SurfaceHolder surfaceHolder)void setVideoSurfaceView(@Nullable SurfaceView surfaceView)void setVideoTextureView(@Nullable TextureView textureView)
There are also different ways to clear it:
void clearVideoSurface()void clearVideoSurface(@Nullable Surface surface)void clearVideoSurfaceHolder(@Nullable SurfaceHolder surfaceHolder)void clearVideoSurfaceView(@Nullable SurfaceView surfaceView)void clearVideoTextureView(@Nullable TextureView textureView)
Choose a surface type for PlayerView
The surface_type attribute of PlayerView lets you set the type of surface
used for video playback. The allowed values are:
surface_view(SurfaceView)texture_view(TextureView)spherical_gl_surface_view(SphericalGLSurfaceView) - for spherical video playbackvideo_decoder_gl_surface_view(VideoDecoderGLSurfaceView) - video rendering using extension renderersnone- which is for audio playback only and should be used to avoid having to create a surface because doing so can be expensive.
If the view is for regular video playback then surface_view or texture_view
should be used. SurfaceView has a number of benefits over TextureView
for video playback:
- Significantly lower power consumption on many devices.
- More accurate frame timing, resulting in smoother video playback.
- Support for higher quality HDR video output on capable devices.
- Support for secure output when playing DRM-protected content.
- The ability to render video content at the full resolution of the display on Android TV devices that upscale the UI layer.
SurfaceView should therefore be preferred over TextureView where possible.
TextureView should be used only if SurfaceView does not meet your needs—for
example, when you need to apply transformations, alpha blending, or
non-rectangular clipping to the video surface.
Choose a surface type in Compose
In Compose, the interop solution uses the AndroidView Composable to wrap
SurfaceView and TextureView. The two Composables that correspond to that
are AndroidExternalSurface and AndroidEmbeddedExternalSurface from
androidx.compose.foundation. However, these proxy classes provide an API
surface that limits access of the underlying views. Those views are needed by
the Player to handle a full lifecycle of the surface (creation and size
updates).
In media3-ui-compose module, you can find ContentFrame and
PlayerSurface composables that link the Player to a Surface in a
lifecycle-aware manner. The surface types in this case are:
androidx.media3.ui.compose.SURFACE_TYPE_SURFACE_VIEWandroidx.media3.ui.compose.SURFACE_TYPE_TEXTURE_VIEW
There is no type none, since that would correspond to not including the
composable in your Compose UI tree.
To learn how to manage video aspect ratios, dynamic window sizing, and content scaling in Compose, see Scale media content across form factors.