Skip to content

Instantly share code, notes, and snippets.

@robksawyer
Created August 13, 2022 19:07
Show Gist options
  • Save robksawyer/a8311e38b8066e4a95830545657e01be to your computer and use it in GitHub Desktop.
Save robksawyer/a8311e38b8066e4a95830545657e01be to your computer and use it in GitHub Desktop.
This snippet shows how to load audio with Three JS
const [sound, setSound] = useState()
const [listener, setListener] = useState()
const [analyser, setAnalyser] = useState()
const [data, setData] = useState(1)
// load a sound and set it as the Audio object's buffer
useEffect(() => {
const listener = new THREE.AudioListener()
setListener(listener)
const sound = new THREE.PositionalAudio(listener)
setSound(sound)
const analyser = new THREE.AudioAnalyser(sound, 32)
setAnalyser(analyser)
const audioLoader = new THREE.AudioLoader()
audioLoader.load('/audio/afterparty.mp3', (buffer) => {
sound.setBuffer(buffer)
sound.setLoop(true)
sound.setVolume(12)
sound.play()
})
}, [])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment