Skip to content

Instantly share code, notes, and snippets.

@simonghales
Last active January 20, 2021 09:35
Show Gist options
  • Save simonghales/122e06258e465b428046edcfbbab69a8 to your computer and use it in GitHub Desktop.
Save simonghales/122e06258e465b428046edcfbbab69a8 to your computer and use it in GitHub Desktop.
threejs positional audio with stream via web sockets

Just chucking up a random gist to document that if you're trying to create a positional audio in threejs via a stream that's passed in remotely (i.e. not initiated within the current browser) then you will need to create a new Audio (not threejs Audio, the native browser Audio) object and set the srcObject to the stream.

I suspect this activates the stream. Whereas a locally initiated stream is already active, hence why some of the examples I found demo'ing setMediaStreamSource worked, because the stream was local.

So here's my code to demonstrate this.

const audioObj = new Audio()
audioObj.srcObject = stream
const audio = new PositionalAudio(listener)
audio.setMediaStreamSource(stream)

Hope that this might be of help.

Some additional key phrases for anyone googling:

ThreeJS positional audio stream not working

ThreeJS setMediaStreamSource stream not working

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment