Skip to content

Commit 584c3a6

Browse files
yaustarsteveny-sc
andauthored
Video texture example now autoplays on Android and iOS (playcanvas#3318)
video element may have been created incorrectly and also wasn't in browser view Co-authored-by: Steven Yau <syau@snapchat.com>
1 parent d3af9ba commit 584c3a6

File tree

1 file changed

+21
-15
lines changed

1 file changed

+21
-15
lines changed

examples/src/examples/graphics/video-texture.tsx

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -55,21 +55,28 @@ class VideoTextureExample extends Example {
5555
addressV: pc.ADDRESS_CLAMP_TO_EDGE
5656
});
5757

58-
// Grab our HTML element with the video
58+
// Create our HTML element with the video
5959
const video: HTMLVideoElement = document.createElement('video');
60-
video.setAttribute('autoplay', 'true');
61-
video.setAttribute('loop', 'true');
62-
video.setAttribute('muted', 'true');
63-
video.setAttribute('playsinline', 'true');
64-
video.setAttribute('crossorigin', 'anonymous');
65-
video.setAttribute('style', 'display:none');
66-
video.setAttribute('id', 'vid');
67-
const videoSource: HTMLSourceElement = document.createElement('source');
68-
videoSource.setAttribute('src', 'static/assets/video/SampleVideo_1280x720_1mb.mp4');
69-
videoSource.setAttribute('type', 'video/mp4');
70-
video.append(videoSource);
71-
document.body.append('video');
72-
video.addEventListener('canplay', function () {
60+
video.id = 'vid';
61+
video.loop = true;
62+
63+
// Muted so that we can autoplay
64+
video.muted = true;
65+
video.autoplay = true;
66+
67+
// Inline needed for iOS otherwise it plays at fullscreen
68+
video.playsInline = true;
69+
70+
video.crossOrigin = "anonymous";
71+
72+
// Make sure that the video is in view on the page otherwise it doesn't
73+
// load on some browsers, especially mobile
74+
video.setAttribute('style', 'display: block; width: 1px; height: 1px; position: absolute; opacity: 0; z-index: -1000; top: 0px; pointer-events: none');
75+
76+
video.src = 'static/assets/video/SampleVideo_1280x720_1mb.mp4';
77+
document.body.append(video);
78+
79+
video.addEventListener('canplaythrough', function () {
7380
videoTexture.setSource(video);
7481
});
7582

@@ -87,7 +94,6 @@ class VideoTextureExample extends Example {
8794
entity.render.meshInstances[1].material = material;
8895

8996
video.load();
90-
// video.play();
9197

9298
const mouse = new pc.Mouse(document.body);
9399
mouse.on('mousedown', function (event) {

0 commit comments

Comments
 (0)