Skip to content

UI Video

Updated

A video element.

Type
ent-ui-video
Script class
UIVideoEntity
Family
UI

A UI Video plays a video on a surface inside a UI: a trailer in a menu, a tutorial clip in a help panel, a live stream in a lobby screen. Its UI Video component (comp-ui-video) plays from src, a URL or one of the project’s video assets, and fits the picture into its box like an image. It plays the same sources as the world-space Video, including live streams through a Media Stream component, and a Chroma Key can make a green screen transparent.

A UI Video sits nested in a panel or layout, usually with play and volume controls beside it. A new one starts playing by itself, muted and looping, because browsers block autoplay with sound until the player has interacted. It never plays while you edit.

With a Signal Emitter on its entity, it reports UIVideo::ready { duration } when the first frame has loaded (in seconds, Infinity for a live stream), UIVideo::error { message }, and UIVideo::play, UIVideo::pause, UIVideo::stop and UIVideo::end.

Use a UI Video for video inside a UI panel. For a large screen that is part of the 3D world, use the world-space Video entity.

The props that matter most:

  • src: the video.
  • autoplay, muted and repeat: start by itself (on), start muted (on), and 'repeat' or play 'once'.
  • volume and playbackRate: the gain from 0 to 1, and the speed from 0.5 to 4.
  • width, height and objectFit: the surface size in layout pixels (px), and 'cover' (crop to fill) or 'fill' (stretch).

From a script, play(), pause(), stop(), setVolume(volume), getVolume() and isPlaying() control playback.

  1. Add a UI Video under a UI Panel or UI Layout. See Scenes and hierarchy.
  2. Set src, a width and height, and repeat.
  3. Turn autoplay off if a button should start it.
  4. Add a Signal Emitter to hear its playback events, and controls (buttons, a slider) beside it.

Goal: two buttons start the video with sound and stop it.

TrailerControls.jx
#pragma lifecycle(startup)
// Scene: a UI Video named 'Trailer' (autoplay off), and UI Buttons named 'Play Trailer'
// and 'Stop Trailer' with Signal Emitters on 'trailer:play' and 'trailer:stop'.
function startup() {
const video = findEntity('Trailer').getComponent(UIVideoComponent);
on('trailer:play', (event) => {
if (event.eventType !== EventType.UIButton.Press) return;
video.setProps({ muted: false });
video.play();
});
on('trailer:stop', (event) => {
if (event.eventType === EventType.UIButton.Press) video.stop();
});
}

How it works:

  • Unmuting happens inside a press, after the player has interacted, so the browser allows sound.
  • play() starts or resumes; stop() stops and goes back to the start.

Show the length, offer a replay at the end

Section titled “Show the length, offer a replay at the end”

Goal: show the clip’s length when it is ready, and a Replay button when it ends.

TutorialClip.jx
#pragma lifecycle(startup)
// Scene: a UI Video named 'Tutorial' (repeat: 'once', Signal Emitter: 'tutorial:video'),
// a UI Label named 'Clip Info', and a UI Button named 'Replay' (Signal Emitter: 'tutorial:replay').
function startup() {
const video = findEntity('Tutorial').getComponent(UIVideoComponent);
const info = findEntity('Clip Info').getComponent(UILabelComponent);
const replay = findEntity('Replay');
replay.setEnabled(false);
on('tutorial:video', (event) => {
if (event.eventType === EventType.UIVideo.Ready) {
info.setProps({ text: `Tutorial · ${Math.round(event.data.duration)} s` });
}
if (event.eventType === EventType.UIVideo.End) replay.setEnabled(true);
if (event.eventType === EventType.UIVideo.Error) info.setProps({ text: 'The video could not load.' });
});
on('tutorial:replay', (event) => {
if (event.eventType !== EventType.UIButton.Press) return;
replay.setEnabled(false);
video.stop();
video.play();
});
}

How it works:

  • The listeners are registered in startup: the first ready and autoplay play events arrive after Behaviors start, so none is missed.
  • With repeat: 'once', UIVideo::end arrives at the end, and the Replay button appears.
  • stop() then play() starts the clip again from the beginning.

Goal: a slider beside the video sets its volume.

VideoVolume.jx
#pragma lifecycle(startup)
// Scene: a UI Video named 'Screen' and a UI Slider named 'Screen Volume'
// with a Signal Emitter on 'screen:volume'.
function startup() {
const video = findEntity('Screen').getComponent(UIVideoComponent);
const slider = findEntity('Screen Volume').getComponent(UISliderComponent);
slider.setProps({ text: 'Volume', min: 0, max: 100, step: 5, unit: '%' });
slider.setValue(Math.round(video.getVolume() * 100));
on('screen:volume', (event) => {
if (event.eventType !== EventType.UISlider.Change) return;
video.setProps({ muted: event.data.value === 0 });
video.setVolume(event.data.value / 100);
});
}

How it works:

  • The slider starts at the video’s current volume, read with getVolume().
  • Each step sets the gain from 0 to 1; moving off 0 unmutes the video, so the first drag is heard.

What a new UI Video is made of. Required components are always present and cannot be removed; the others can. Per entity says whether an entity can hold one of that component or several. Layer, Lock and Enable are on every placed entity, and Transform holds its position, rotation and scale.

Component Type Role Per entity What it does
Layer comp-layer Required One Puts the entity on a render/raycast layer so cameras and rays can include or skip it.
Lock comp-lock Required One Editor-only flag that stops the entity from being selected or moved.
Enable comp-enable Required One Turns the entity and its children on or off: hides them and stops their behavior.
Transform comp-transform Required One Position, rotation and scale of the entity, with optional editor-only per-axis locks.
UI Video comp-ui-video Required One A video element.

You can also add 7 optional components, such as AI Speech Capture, Attribution, Chroma Key, Media Stream, Networkable, Networkable Trait.

The settings of a new entity, as the runtime creates it. Change them in the Properties panel of XR Creator, or from a script with getComponent(…).setProps({ … }).

Prop Default Range
objectFit "cover"
keepAspectRatio true
volume 1 0 ≤ x ≤ 1
muted true
repeat "repeat"
autoplay true
playbackRate 1 0.5 ≤ x ≤ 4
crossOrigin "anonymous"
restart true

With a Signal Emitter on the entity, these events reach scripts as { entityId, eventType, data }. Compare eventType with the EventType constants.

  • UIVideo::ready
  • UIVideo::error
  • UIVideo::play
  • UIVideo::pause
  • UIVideo::stop
  • UIVideo::end

Like every entity, it also reports: Transform::change (and position-change, rotation-change, scale-change), Enable::change, Layer::change and Lock::change.

Run it in the browser, read its scripts, and download it as an XR Creator project.

Find the entity by its name, check its class, and work with its components. Name the entity in the Hierarchy first; this example expects My UI Video.

UIVideoEntity.jx
#pragma lifecycle(startup)
function startup() {
// Find the entity by the name it has in the Hierarchy.
const entity = findEntity('My UI Video');
if (!(entity instanceof UIVideoEntity)) return;
// Its UI Video component holds the settings listed under Props.
const component = entity.getComponent(UIVideoComponent);
log('volume is', component.props.volume);
component.setProps({ volume: 1 });
}