Jetpack XR SDK 支援在平面上播放立體並排影片。立體影片的每個影格都包含左眼和右眼圖片,可讓觀眾感受到深度,這也稱為立體視覺。
您可以在 Android XR 應用程式中,使用用於其他板型規格的 Android 開發的標準媒體 API,算繪非立體 2D 影片。
使用 Jetpack SceneCore 播放並排影片
在並排影片中,每個立體影格會以兩張圖片的形式呈現,並水平排列。頂部和底部的影片影格會以垂直相鄰的方式排列。
並排視訊不是轉碼器,而是一種安排立體影格的方式,也就是說,它可以使用 Android 支援的任何轉碼器進行編碼。
您可以使用 Media3 Exoplayer 載入並並排顯示影片,然後使用新的 SurfaceEntity
進行轉譯。如要建立 SurfaceEntity
,請呼叫 SurfaceEntity.create
,如以下範例所示。
val stereoSurfaceEntity = SurfaceEntity.create( xrSession, SurfaceEntity.StereoMode.SIDE_BY_SIDE, Pose(Vector3(0.0f, 0.0f, -1.5f)), SurfaceEntity.CanvasShape.Quad(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( xrSession, SurfaceEntity.StereoMode.MULTIVIEW_LEFT_PRIMARY, Pose(Vector3(0.0f, 0.0f, -1.5f)), SurfaceEntity.CanvasShape.Quad(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 播放 180 度和 360 度影片
SurfaceEntity
支援在半球形表面上播放 180 度影片,以及在球形表面上播放 360 度影片。radius
參數預設為各個表面的半徑大小 (以公尺為單位)。
以下程式碼說明如何設定 SurfaceEntity
,以便在 180 度半球和 360 度球上播放。使用這些畫布形狀時,請利用使用者的頭部姿勢來定位表面,以提供身歷式體驗。
// Set up the surface for playing a 180° video on a hemisphere. val hemisphereStereoSurfaceEntity = SurfaceEntity.create( xrSession, SurfaceEntity.StereoMode.SIDE_BY_SIDE, xrSession.scene.spatialUser.head?.transformPoseTo( Pose.Identity, xrSession.scene.activitySpace )!!, SurfaceEntity.CanvasShape.Vr180Hemisphere(1.0f), ) // ... and use the surface for playing the media.
// Set up the surface for playing a 360° video on a sphere. val sphereStereoSurfaceEntity = SurfaceEntity.create( xrSession, SurfaceEntity.StereoMode.TOP_BOTTOM, xrSession.scene.spatialUser.head?.transformPoseTo( Pose.Identity, xrSession.scene.activitySpace )!!, SurfaceEntity.CanvasShape.Vr360Sphere(1.0f), ) // ... and use the surface for playing the media.
使用 XR 專用的 Jetpack Compose 播放空間影片
如果您想瞭解如何使用 Jetpack Compose for XR 播放影片,請參閱這篇文章,瞭解如何為圖片或影片內容新增途徑。