在應用程式中加入空間定位影片

適用的 XR 裝置
本指南可協助您為這類 XR 裝置打造體驗。
XR 頭戴式裝置
有線 XR 眼鏡

Jetpack XR SDK 支援在平面上播放立體並排影片。立體影片的每個影格都包含左眼和右眼圖片,讓觀眾產生深度感,也就是立體視覺。

您可以使用標準媒體 API,在 Android XR 應用程式中算繪非立體 2D 影片,這些 API 也用於其他板型規格的 Android 開發作業。

使用 Jetpack SceneCore 並排播放影片

在並排影片中,每個立體影格都會呈現為兩張水平相鄰的圖片。上下影片影格會垂直相鄰排列。

並排影片並非轉碼器,而是立體影格的組織方式,也就是說,可以使用 Android 支援的任何轉碼器進行編碼。

您可以使用 Media3 Exoplayer 載入並排影片,然後使用新的 SurfaceEntity 算繪影片。如要建立 SurfaceEntity,請呼叫 SurfaceEntity.create,如以下範例所示。

val stereoSurfaceEntity = SurfaceEntity.create(
    session = xrSession,
    stereoMode = SurfaceEntity.StereoMode.SIDE_BY_SIDE,
    pose = Pose(Vector3(0.0f, 0.0f, -1.5f)),
    shape = SurfaceEntity.Shape.Quad(FloatSize2d(1.0f, 1.0f))
)
val videoUri = Uri.Builder()
    .scheme(ContentResolver.SCHEME_ANDROID_RESOURCE)
    .path("sbs_video.mp4")
    .build()
val mediaItem = MediaItem.fromUri(videoUri)

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

使用 Jetpack SceneCore 播放 MV-HEVC 影片

MV-HEVC 轉碼器標準經過最佳化,專為立體影片設計,可讓應用程式有效率地播放高畫質沉浸式影片。MV-HEVC 檔案有一個主要串流 (通常是左眼),以及另一個眼睛的立體聲串流。

與並排影片類似,您可以使用 Media3 Exoplayer 載入,並使用 SurfaceEntity 算繪。呼叫 SurfaceEntity.create 時,請在 stereoMode 參數中指定 MV-HEVC 檔案是以左眼或右眼為主。

// Create the SurfaceEntity with the StereoMode corresponding to the MV-HEVC content
val stereoSurfaceEntity = SurfaceEntity.create(
    session = xrSession,
    stereoMode = SurfaceEntity.StereoMode.MULTIVIEW_LEFT_PRIMARY,
    pose = Pose(Vector3(0.0f, 0.0f, -1.5f)),
    shape = SurfaceEntity.Shape.Quad(FloatSize2d(1.0f, 1.0f))
)
val videoUri = Uri.Builder()
    .scheme(ContentResolver.SCHEME_ANDROID_RESOURCE)
    .path("mvhevc_video.mp4")
    .build()
val mediaItem = MediaItem.fromUri(videoUri)

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

使用 Jetpack SceneCore 播放受 DRM 保護的空間影片

Jetpack XR SDK 支援使用 Android 內建的數位著作權管理 (DRM) 架構播放加密影片串流。DRM 可確保內容安全發布,並防止未經授權的複製或播放行為,進而保護內容。

這個程序涉及媒體播放器應用程式與授權伺服器聯絡,以取得解密金鑰。在 Android 系統中,這項程序會以安全的方式管理,解密的影片影格會轉譯至受保護的圖形緩衝區,系統或其他應用程式無法存取,因此無法擷取螢幕畫面。

如要使用 Jetpack SceneCore 播放受 DRM 保護的影片,請按照下列步驟操作:

  1. 設定 SurfaceEntity 來要求受保護的介面。
  2. 使用必要的 DRM 資訊設定 Media3 Exoplayer,以處理金鑰交換。
  3. 將播放器的輸出內容設為 SurfaceEntity 的介面。

以下範例說明如何設定 ExoPlayer,播放受 DRM 保護的串流,並在 SurfaceEntity 上算繪:

// Create a SurfaceEntity with DRM content

// Define the URI for your DRM-protected content and license server.
val videoUri = "https://your-content-provider.com/video.mpd"
val drmLicenseUrl = "https://your-license-server.com/license"

// Create the SurfaceEntity with the PROTECTED content security level.
val protectedSurfaceEntity = SurfaceEntity.create(
    session = xrSession,
    stereoMode = SurfaceEntity.StereoMode.SIDE_BY_SIDE,
    pose = Pose(Vector3(0.0f, 0.0f, -1.5f)),
    shape = SurfaceEntity.Shape.Quad(FloatSize2d(1.0f, 1.0f)),
    surfaceProtection = SurfaceEntity.SurfaceProtection.PROTECTED
)

// Build a MediaItem with the necessary DRM configuration.
val mediaItem = MediaItem.Builder()
    .setUri(videoUri)
    .setDrmConfiguration(
        MediaItem.DrmConfiguration.Builder(C.WIDEVINE_UUID)
            .setLicenseUri(drmLicenseUrl)
            .build()
    )
    .build()

// Initialize ExoPlayer and set the protected surface.
val exoPlayer = ExoPlayer.Builder(this).build()
exoPlayer.setVideoSurface(protectedSurfaceEntity.getSurface())

// Set the media item and start playback.
exoPlayer.setMediaItem(mediaItem)
exoPlayer.prepare()
exoPlayer.play()

如要進一步瞭解 Android 的媒體 DRM 架構,請參閱 source.android.com 上的媒體 DRM 說明文件。

使用 Jetpack SceneCore 播放 180 度和 360 度影片

SurfaceEntity 支援在半球形表面播放 180° 影片,以及在球形表面播放 360 度影片。radius 參數預設是指各個表面的半徑大小 (以公尺為單位)。

以下程式碼說明如何設定 SurfaceEntity,在 180° 半球和 360° 球體上播放。使用這些畫布形狀時,請根據使用者的頭部姿勢調整介面位置,提供沉浸式體驗。

val devicePose = ArDevice.getInstance(xrSession).state.value.devicePose
val activitySpacePose = xrSession.scene.perceptionSpace.transformPoseTo(devicePose, xrSession.scene.activitySpace)

// Set up the surface for playing a 180° video on a hemisphere.
val hemisphereStereoSurfaceEntity =
    SurfaceEntity.create(
        session = xrSession,
        stereoMode = SurfaceEntity.StereoMode.SIDE_BY_SIDE,
        pose = activitySpacePose,
        shape = SurfaceEntity.Shape.Hemisphere(1.0f),
    )
// ... and use the surface for playing the media.

val devicePose = ArDevice.getInstance(xrSession).state.value.devicePose
val activitySpacePose = xrSession.scene.perceptionSpace.transformPoseTo(devicePose, xrSession.scene.activitySpace)
// Set up the surface for playing a 360° video on a sphere.
val sphereStereoSurfaceEntity =
    SurfaceEntity.create(
        session = xrSession,
        stereoMode = SurfaceEntity.StereoMode.TOP_BOTTOM,
        pose = activitySpacePose,
        shape = SurfaceEntity.Shape.Sphere(1.0f),
    )
// ... and use the surface for playing the media.

進階 SurfaceEntity 控制項

如要進一步控管影片和圖片算繪,例如套用自訂材質效果,可以直接使用 SceneCore 程式庫中的 SurfaceEntity。

以下各節說明 SurfaceEntity 提供的部分進階功能。

套用邊緣柔邊

設定 edgeFeatheringParams 屬性,柔化表面邊緣,使其與環境融合。

// Create a SurfaceEntity.
val surfaceEntity = SurfaceEntity.create(
    session = xrSession,
    pose = Pose(Vector3(0.0f, 0.0f, -1.5f))
)

// Feather the edges of the surface.
surfaceEntity.edgeFeatheringParams =
    SurfaceEntity.EdgeFeatheringParams.RectangleFeather(0.1f, 0.1f)

套用 Alpha 遮罩

套用 Alpha 遮罩,建立非矩形表面或加入透明效果。首先,從素材資源載入 Texture,然後指派給 primaryAlphaMaskTexture 屬性:

// Create a SurfaceEntity.
val surfaceEntity = SurfaceEntity.create(
    session = xrSession,
    pose = Pose(Vector3(0.0f, 0.0f, -1.5f))
)

// Load the texture in a coroutine scope.
activity.lifecycleScope.launch {
    val alphaMaskTexture =
        Texture.create(
            xrSession,
            Paths.get("textures", "alpha_mask.png"),
        )

    // Apply the alpha mask.
    surfaceEntity.primaryAlphaMaskTexture = alphaMaskTexture

    // To remove the mask, set the property to null.
    surfaceEntity.primaryAlphaMaskTexture = null
}

使用 Jetpack Compose for XR 播放空間影片

如要瞭解如何使用 Jetpack Compose for XR 播放影片,請參閱「新增圖片或影片內容的介面」。