Audio Streaming
Play live audio streams in your scene.
You can stream audio from a URL. This is useful to play music directly from an internet radio, or stream a conference into your scene.
The audio in the source must be in one of the following formats: .mp3, ogg, or aac. The source must also be an https URL (http URLs aren't supported), and the source should have CORS policies (Cross Origin Resource Sharing) that permit externally accessing it. If this is not the case, you might need to set up a server to act as a proxy and expose the stream in a valid way.
π Note: To instead play a pre-recorded sound in your scene, see Sounds.
To add an audio stream into your scene, simply add an AudioStream component to an entity:
const streamEntity = engine.addEntity()
AudioStream.create(streamEntity, {
url: 'https://icecast.ravepartyradio.org/ravepartyradio-192.mp3',
playing: true,
volume: 0.8,
})π Note: The streamed sound isn't positional, it will be heard at a consistent volume throughout your entire scene. If a player steps out of the scene, they will not hear the streaming at all.
Set the volume of the AudioStream component by changing its volume property.
Switch the AudioStream component on or off by setting its playing property to true or false.
Stream state
Query the state of an audio stream using the function AudioStream.getAudioState(), passing the entity that owns the AudioStream component.
The returned state is a value of the MediaState enum. This enum has the following possible values:
MS_BUFFERINGMS_ERRORMS_LOADINGMS_NONEMS_PAUSEDMS_PLAYINGMS_READYMS_SEEKING
The following example checks on the state of a stream, and logs when there's a change.
Spatial audio
By default, the audio from an AudioStream component is global, meaning it will be heard at a consistent volume throughout your entire scene. If a player steps out of the scene, they will not hear the streaming at all.
To make the audio spatial, set the spatial property to true.
The audio will now be heard from the position of the entity that owns the AudioStream component, and will be louder as the player approaches it.
Control the spatial audio with the following properties:
spatialMinDistance: The minimum distance at which audio becomes spatial. If the player is closer, the audio will be heard at full volume. 0 by default.spatialMaxDistance: The maximum distance at which the audio is heard. If the player is further away, the audio will be heard at 0 volume. 60 by default
π Note: Some audio formats don't support spatial audio. Make sure the stream audio is encoded in mp3, AAC-LC or FLAC.
Last updated