Pause and resume music
Updated
Your first sound: background music on an Audio entity, and a radio that pauses and resumes it when clicked, with the source’s own events in the log.
With your sound on, click the radio to pause the music, and again to resume it.
Pause and resume music
#pragma lifecycle(startup)
let music;
function startup() { music = findEntity('Music').getComponent(AudioComponent);
// play() after pause() resumes from where it stopped. on('radio/clicked', (event) => { if (event.eventType !== EventType.Selectable.Select) return; findEntity(event.entityId)?.getComponent(SelectableComponent)?.setSelected(false); // so the next click selects it again if (music.isPlaying()) music.pause(); else music.play(); });
on('music', (event) => { if (event.eventType === EventType.Audio.Ready) log(`Music loaded: ${event.data.duration.toFixed(1)} s`); if (event.eventType === EventType.Audio.Play) log('Music playing'); if (event.eventType === EventType.Audio.Pause) log('Music paused'); });}How it works
Section titled “How it works”The script is explained step by step in Pause and resume music.
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.
FirstSound.xrproj