Skip to content

Commit 015df95

Browse files
authored
Migrate snippets from "Add spatial video to your app" (#505)
1 parent 83e8533 commit 015df95

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed

xr/build.gradle.kts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ dependencies {
3636
implementation(libs.guava)
3737
implementation(libs.kotlinx.coroutines.guava)
3838

39+
implementation(libs.androidx.media3.exoplayer)
40+
3941
val composeBom = platform(libs.androidx.compose.bom)
4042
implementation(composeBom)
4143

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* Copyright 2025 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.xr.scenecore
18+
19+
import android.content.ContentResolver
20+
import android.net.Uri
21+
import androidx.activity.ComponentActivity
22+
import androidx.media3.common.MediaItem
23+
import androidx.media3.exoplayer.ExoPlayer
24+
import androidx.xr.runtime.math.Pose
25+
import androidx.xr.runtime.math.Vector3
26+
import androidx.xr.scenecore.Session
27+
import androidx.xr.scenecore.StereoSurfaceEntity
28+
29+
private fun ComponentActivity.surfaceEntityCreate(xrSession: Session) {
30+
// [START androidxr_scenecore_surfaceEntityCreate]
31+
val stereoSurfaceEntity = StereoSurfaceEntity.create(
32+
xrSession,
33+
StereoSurfaceEntity.StereoMode.SIDE_BY_SIDE,
34+
// Position 1.5 meters in front of user
35+
Pose(Vector3(0.0f, 0.0f, -1.5f)),
36+
StereoSurfaceEntity.CanvasShape.Quad(1.0f, 1.0f)
37+
)
38+
val videoUri = Uri.Builder()
39+
.scheme(ContentResolver.SCHEME_ANDROID_RESOURCE)
40+
.path("sbs_video.mp4")
41+
.build()
42+
val mediaItem = MediaItem.fromUri(videoUri)
43+
44+
val exoPlayer = ExoPlayer.Builder(this).build()
45+
exoPlayer.setVideoSurface(stereoSurfaceEntity.getSurface())
46+
exoPlayer.setMediaItem(mediaItem)
47+
exoPlayer.prepare()
48+
exoPlayer.play()
49+
// [END androidxr_scenecore_surfaceEntityCreate]
50+
}
51+
52+
private fun ComponentActivity.surfaceEntityCreateSbs(xrSession: Session) {
53+
// [START androidxr_scenecore_surfaceEntityCreateSbs]
54+
// Set up the surface for playing a 180° video on a hemisphere.
55+
val hemisphereStereoSurfaceEntity =
56+
StereoSurfaceEntity.create(
57+
xrSession,
58+
StereoSurfaceEntity.StereoMode.SIDE_BY_SIDE,
59+
xrSession.spatialUser.head?.transformPoseTo(
60+
Pose.Identity,
61+
xrSession.activitySpace
62+
)!!,
63+
StereoSurfaceEntity.CanvasShape.Vr180Hemisphere(1.0f),
64+
)
65+
// ... and use the surface for playing the media.
66+
// [END androidxr_scenecore_surfaceEntityCreateSbs]
67+
}
68+
69+
private fun ComponentActivity.surfaceEntityCreateTb(xrSession: Session) {
70+
// [START androidxr_scenecore_surfaceEntityCreateTb]
71+
// Set up the surface for playing a 360° video on a sphere.
72+
val sphereStereoSurfaceEntity =
73+
StereoSurfaceEntity.create(
74+
xrSession,
75+
StereoSurfaceEntity.StereoMode.TOP_BOTTOM,
76+
xrSession.spatialUser.head?.transformPoseTo(
77+
Pose.Identity,
78+
xrSession.activitySpace
79+
)!!,
80+
StereoSurfaceEntity.CanvasShape.Vr360Sphere(1.0f),
81+
)
82+
// ... and use the surface for playing the media.
83+
// [END androidxr_scenecore_surfaceEntityCreateTb]
84+
}

0 commit comments

Comments
 (0)