HomeGuidesAPI ReferenceChangelogDiscussions
Hire a ReactVision Expert 🛠️Launch Studio 🖥️Sponsor on GitHub ❤️Log In
Guides

ViroSound

A component that enables the user play and control mono and stereo sound effects. Refer to Sound under Develop for more information.

Web, 3.2.2+: sounds play through Web Audio, so they mix with a video that has sound, which on iPhone they did not before. A sound the browser holds back until the user has tapped, clicked or pressed a key starts at that first gesture. The page asks for a playback audio session, so the ring/silent switch does not mute it on iOS.

Web, 3.2.3+: seekToTime, onBufferStart, onBufferEnd, onUpdateTime, preloadSounds and unloadSounds work. Asking for the microphone after a ViroSound works on Safari.

3.4.0+: for a cue fired often (a hit, a pop, a footstep), use ViroSound.playOneShot instead of mounting a ViroSound per cue. See also ViroSpatialSound.playOneShot.

Example use:
<ViroSound
    source={require("./sound/mysound.mp3")}
    onFinish={this.onFinishSound}
/>

Props

source (* required)

TypeDescription
ImageSourcePropTypeThe sound source, either a mono or stereo audio file. Supported extensions include .mp3 and .wav.This can also be the key of a preloaded sound. See preloadSounds below for more information.

An asset can be loaded by using require() or { uri: 'https://example.com/your-sound.mp3' }

loop

TypeDescription
booleanSet to true to loop the sound. This is set to false by default.

muted

TypeDescription
booleanSet to true to mute the sound. This is set to false by default.

onError

TypeDescription
FunctionCallback invoked when the Sound fails to load. The error message is contained in event.nativeEvent.error

onFinish

TypeDescription
FunctionCallback that is called when the sound is finished playing. This function isn't called at the end of a sound if looping is enabled.

paused

TypeDescription
booleanSet to true to pause the sound. This is set to false by default.

volume

TypeDescription
numberA number represented volume from 0 to 1. Max volume is equal to 1. Min volume is equal to 0. This is set to 1 by default.

Methods

seekToTime(timeInSeconds)

Seek to the given point in the Sound, in seconds.

Parameters

timeInSeconds - The seek position in seconds. From 3.1.0 fractional seconds are honoured; before, they were truncated to whole seconds.

Static Methods

static preloadSounds(soundMap:{[key:string]: string)

Given a map of keys and links to their corresponding sound data, Viro will prefetch each sound and store it locally for quick access, asynchronously. You can then play these sounds later by providing the key in the sound's source attribute. We currently only support external urls (web-based).

The promise resolves with each key's { result, msg }.

Android, 3.4.0+: a bundled sound (require()) preloads in a release build. There a require()d sound is a resource name with no scheme, which was downloaded as a URL and failed (IO error downloading file at URL [assets_audio_…], { result: false, msg: "Failed to load sound" } for every entry). It is now resolved to the app's resource, as ViroSound's own source is.

iOS, 3.2.0+: preloading works. Before 3.2.0 it never loaded anything and its promise never resolved, and a source={{ name }} sound stayed silent. The data is now loaded and kept under its key, the promise resolves with { result, msg } as on Android, and a named sound plays from it, firing onFinish. A downloaded sound also opens now.

Example showing the preloading of two sounds:

ViroSound.preloadSounds({  
  "cube_sound": resolveAssetSource(require("../res/metronome.mp3")),
  "cube_sound_2": "http://www.kozco.com/tech/32.mp3"),
});

static unloadSounds(soundKeys: [string])

Given sound keys, will delete the local prefetched copy of sound data from the application's internal directory.

ViroSound.unloadSounds([ "cube_sound" ]);

static playOneShot(name, options?) (3.4.0+)

Promise<boolean>. Plays a clip preloaded with preloadSounds once, head-locked, with no component mounted for it. A ViroSound per cue creates a native sound, copies the clip and prepares a player each time, on the main thread, which is what made a game with many cues lag on Quest. A one-shot decodes its clip into the audio engine the first time and plays from memory after that, with nothing on the main thread. Up to 8 plays of one clip sound at once; one more ends the oldest.

ParameterTypeDescription
namestringA key passed to preloadSounds.
optionsViroSoundOneShotOptions{ volume?: number }, 0 to 1, default 1.

Resolves true when the clip was handed to the audio engine and false when it was not (the name was not preloaded, no scene is mounted, or on iOS the clip has not finished loading), with the reason in the native log. It never rejects, so it can be called and forgotten.

Platforms: Android and Meta Quest, and iOS. On visionOS (one warning) and the web it resolves false: there is no one-shot path there yet. ViroSoundOneShotOptions is exported.

await ViroSound.preloadSounds({ pop: resolveAssetSource(require("./res/pop.mp3")) });
// later, as often as needed:
ViroSound.playOneShot("pop", { volume: 0.8 });

For a clip placed in the world, use ViroSpatialSound.playOneShot.