Created
August 13, 2022 19:07
-
-
Save robksawyer/a8311e38b8066e4a95830545657e01be to your computer and use it in GitHub Desktop.
This snippet shows how to load audio with Three JS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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