向应用添加空间视频

Jetpack XR SDK 支持在平面上播放立体并排视频。立体视频的每个帧都包含左眼和右眼图像,以便观看者感受到深度。

您可以使用用于在其他类型设备上进行 Android 开发的标准媒体 API,在 Android XR 应用中渲染非立体 2D 视频。

使用 Jetpack XR SDK 并排播放视频

在并排视频中,每个立体图像帧都显示为两张图片,水平排列在一起。顶部和底部的视频帧垂直排列,彼此相邻。

并排视频不是编解码器,而是组织立体声帧的方式,这意味着它可以使用 Android 支持的任何编解码器进行编码。

您可以使用 Media3 Exoplayer 加载并排视频,然后使用新的 StereoSurfaceEntity 进行渲染。如需创建 StereoSurfaceEntity,请调用 StereoSurfaceEntity.create,如以下示例所示。

stereoSurfaceEntity = StereoSurfaceEntity.create(
            xrSession,
            StereoSurfaceEntity.StereoMode.SIDE_BY_SIDE,
            // Position 1.5 meters in front of user
            Pose(Vector3(0.0f, 0.0f, -1.5f), Quaternion(0.0f, 0.0f, 0.0f, 1.0f)),
            StereoSurfaceEntity.CanvasShape.Quad(1.0f, 1.0f)
        )
        val videoUri = Uri.Builder()
            .scheme(ContentResolver.SCHEME_ANDROID_RESOURCE)
            .path(R.raw.sbs_test_video.toString())
            .build()
        val mediaItem = MediaItem.fromUri(videoUri)

exoPlayer = ExoPlayer.Builder(this).build()
exoPlayer.setVideoSurface(stereoSurfaceEntity.getSurface())
exoPlayer.setMediaItem(mediaItem)
exoPlayer.prepare()
exoPlayer.play()

使用 Jetpack XR SDK 播放 180 度和 360 度视频

Alpha02 及更高版本

从版本 1.0.0-alpha02 开始,StereoSurfaceEntity 支持在半球形表面上播放 180 度视频,在球形表面上播放 360 度视频。如果视频是立体视频,则文件应采用并排格式。

以下代码展示了如何设置 StereoSurfaceEntity,以便在 180° 半球和 360° 球面上播放。使用这些画布形状时,请利用用户的头部姿势来定位 Surface,以提供沉浸式体验。

// Set up the surface for playing a 180° video on a hemisphere.
hemisphereStereoSurfaceEntity =
    StereoSurfaceEntity.create(
        xrCoreSession,
        StereoSurfaceEntity.StereoMode.SIDE_BY_SIDE,
        xrCoreSession.spatialUser.head?.transformPoseTo(
            Pose.Identity,
            xrCoreSession.activitySpace
        )!!,
        StereoSurfaceEntity.CanvasShape.Vr180Hemisphere(1.0f),
    )
// ... and use the surface for playing the media.
// Set up the surface for playing a 360° video on a sphere.
sphereStereoSurfaceEntity =
    StereoSurfaceEntity.create(
        xrCoreSession,
        StereoSurfaceEntity.StereoMode.TOP_BOTTOM,
        xrCoreSession.spatialUser.head?.transformPoseTo(
            Pose.Identity,
            xrCoreSession.activitySpace
        )!!,
        StereoSurfaceEntity.CanvasShape.Vr360Sphere(1.0f),
    )
// ... and use the surface for playing the media.