A playlist
Updated
A jukebox plays three tracks one after another, moving on at the end event; N and P skip forward and back.
Click once to let the browser start the sound. Then press N for the next track and P for the previous one. Drag to orbit the camera, scroll to zoom.
A playlist
#pragma lifecycle(startup, update)
// Scene: an Audio entity "Jukebox" (repeat: once, autoplay: off)// with a Signal Emitter on the signal 'jukebox'.const TRACKS = [ '/example-assets/ambient.mp3', '/example-assets/beat-loop.wav', '/example-assets/rain-loop.wav',];let jukebox;let index = 0;let switching = false;
async function playTrack(i) { switching = true; index = (i + TRACKS.length) % TRACKS.length; jukebox.stop(); jukebox.setProps({ src: TRACKS[index] }); await jukebox.load(); jukebox.play(); switching = false; log(`Now playing track ${index + 1} of ${TRACKS.length}`);}
function startup() { jukebox = findEntity('Jukebox').getComponent(AudioComponent);
on('jukebox', (event) => { if (event.eventType === EventType.Audio.End) playTrack(index + 1); if (event.eventType === EventType.Audio.Error) { log('Skipping a track that cannot load:', event.data.message); playTrack(index + 1); } });
playTrack(0);}
function update() { if (switching) return; if (Input.keyboard.isKeyReleased(Key.N)) playTrack(index + 1); if (Input.keyboard.isKeyReleased(Key.P)) playTrack(index - 1);}How it works
Section titled “How it works”The script is explained step by step in A playlist.
Open it in XR Creator
Section titled “Open it in XR Creator”Download the project file below, then import it in XR Hub with Projects › New › Import, as described in Projects and templates. It opens in XR Creator with the same scene you see running above, each script already attached to its Behavior entity. From there you can edit the scripts, adjust components and their props, preview your changes, and deploy the result as a world of your own.
JukeboxPlaylist.xrproj