SurfaceRenderTexture

Managed SurfaceRenderTexture resource for Android media rendering, this resource obey to the lifecycle management of the Resource, call toGlobal() can make it reusable.

This class provides Android Surface object that can be used as rendering targets for Android media player or other Surface-based renderers.

Usage with VideoMaterial:

  1. Get instance via SurfaceRenderTexture(width,height,maxBufferCount,usageFlag).

  2. Call acquireSurface to get the Android Surface.

  3. Configure media player with the Surface.

  4. Call Surface.release when rendering completes to release the Surface.

  5. Final cleanup via close.

Example:

// Create media player and set data source
val mediaPlayer = MediaPlayer()
mediaPlayer.setDataSource(videoUrl)
mediaPlayer.prepare()

//create surfaceRenderTexture with default size 1920x1080 and maxBufferCount 3 and hold it.
val surfaceRenderTexture = SurfaceRenderTexture()
surfaceRenderTexture.toGlobal()

// Create video renderer work flow with VideoComponent
val videoMaterial = VideoMaterial(
blendingMode = BlendingMode.OPAQUE,
videoDimensionMode = VideoDimensionMode.SIDE_BY_SIDE,
cullingMode = MaterialCullingMode.BACK,
defaultColor = Color4.BLACK
)

videoMaterial.bindSurfaceRenderTexture(surfaceRenderTexture)

if (surfaceRenderTexture.valid) {
val surface = surfaceRenderTexture.acquireSurface()
// Configure media player with the surface
surface?.let {
mediaPlayer.setSurface(it)
}
}

val mesh = MeshResource.createPlane(0.96f, 0.54f)

val videoComponent = VideoComponent(mesh, videoMaterial)

val videoEntity = Entity()
videoEntity.components.set(videoComponent)

// Playback control:
- Start: Call `mediaPlayer.start()`
- Pause: Call `mediaPlayer.pause()`
- Resume: Call `mediaPlayer.resume()`
- Stop: Call `mediaPlayer.stop()`
- Release: Call `mediaPlayer.release()`

// Release Resource when no longer needed:
- videoEntity.destroy()
// or call close to release the resource manually when you want to reuse surfaceRenderTexture.
- surfaceRenderTexture.close()

Notes:

  • Surface objects become invalid after SurfaceRenderTexture is closed.

  • Always stop the video player before closing the surfaceRenderTexture.

See also

The method to bind surfaceRenderTexture to VideoMaterial.

Constructors

Link copied to clipboard
constructor(width: Int = 1920, height: Int = 1080, maxBufferCount: Int = 3, usageFlag: Long = TextureUsageFlag.TEXTURE_USAGE_NONE)

Create a SurfaceRenderTexture resource with the specified width, height, maxBufferCount, and usageFlag.

Functions

Link copied to clipboard
fun acquireSurface(): Surface?

Acquire a surface to render on, which can be used to render video by android media player.

Link copied to clipboard
open override fun close()

You need to manually release the resource to free the memory it occupies.