Skip to content

Android: ViroSpatialSound.playOneShot and ViroSound.playOneShot - #640

Draft
oliedis wants to merge 1 commit into
developfrom
claude/quest-spatial-one-shots
Draft

oliedis wants to merge 1 commit into
developfrom
claude/quest-spatial-one-shots

Conversation

@oliedis

@oliedis oliedis commented Nov 28, 2026

Copy link
Copy Markdown
Member

A fire-and-forget sound API for cues an app fires often, so it does not mount a component per cue. Workaround ID for the apps: viroreact-quest-spatial-one-shots. The renderer side is ReactVision/virocore#477.

Not tested on a device. No Quest was available. examples/quest-sound-one-shots is the scene to check it with.

This does not build until the renderer AAR is rebuilt. The bridge calls SoundData.playOneShot, which is added in virocore#477 and is not in the viro_renderer-release.aar checked into this repository. Merge and build virocore#477 first, then rebuild both AARs here.

Why

Soak Squad moved its one-shot sounds off ViroSound / ViroSpatialSound because each cue mounted a renderer sound: per mount, its Quest logcats show the clip being copied out of the app and a media player being prepared on the Android main thread. Cues grow with the number of enemies and the game lagged.

The API

await ViroSound.preloadSounds({ pop: require("./pop.mp3") });

ViroSpatialSound.playOneShot("pop", [x, y, z], {
  volume: 1,
  rolloffModel: "linear", // "none" (default) | "linear" | "logarithmic"
  minDistance: 0.5,
  maxDistance: 6,
});

ViroSound.playOneShot("pop", { volume: 0.4 }); // head-locked
  • Plays a clip preloaded with ViroSound.preloadSounds, by its key. No component, nothing to unmount.
  • Resolves true when the clip was handed to the audio engine, false when it was not (not preloaded, or no scene mounted). Never rejects, so it can be called and forgotten.
  • The clip is decoded once and played from memory after that. Up to 8 plays of one clip sound at once; one more ends the oldest.
  • Nothing runs on the Android main thread: the call arrives on the native modules thread and the renderer does the rest on a background thread.
  • Android only. On iOS it resolves false with one console warning, and on the web it resolves false. Neither has a one-shot path yet.

How

  • SoundModule.playOneShot(name, position, options, promise) looks the clip up in the preloaded map and calls SoundData.playOneShot on the active ViroContext.
  • There is no component to take a context from, so VRTScene.setViroContext records the context of the scene mounted most recently, held weakly.
  • components/Utilities/ViroSoundOneShot.ts is the shared JavaScript; the two statics call it.

Depends on

Limits

  • The position is fixed when the cue is fired. It cannot follow a moving node.
  • With more than one navigator mounted, cues play through the scene mounted most recently.

Verification

  • npx jest: 1129 tests pass, 5 of them new (__test__/soundOneShot.test.ts). 23 suites fail to load, the same 23 as on develop: they need the sibling viro-web-renderer checkout.
  • tsc --noEmit: no errors outside the web files, which need the same checkout.
  • Java: SoundModule and VRTScene compile with javac against React Native 0.88.1's classes and the bridge classes, with SoundData compiled from virocore#477.
  • Not run: the Gradle build, and no AAR is rebuilt.

To check on a Quest 3

With examples/quest-sound-one-shots (add two short clips; none are shipped), release build, adb logcat -s ReactNativeJS Viro:

  1. preload reports result: true for both clips.
  2. Pops circle the head and a chirp stays centred, at 20 cues a second for 5 minutes; refused stays 0.
  3. One Copied resource line per clip, and no AVPlayer lines while cues fire.
  4. worst tick lateness ms stays low and the frame rate does not drop as cues build up.
  5. Leave to Home and re-enter: cues still play.

Generated by Claude Code

Play a preloaded clip once, at a world position or head-locked, with no component
mounted for it. A component per cue created a native sound and loaded the clip
each time on the main thread. Needs the renderer's SoundData.playOneShot.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JoDeNibskBVXyR57tP3WdH
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants