-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaudio.js
54 lines (44 loc) · 1.33 KB
/
audio.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
const fileButton = document.getElementById("file");
const playButton = document.getElementById("play");
const pauseButton = document.getElementById("pause");
// create an AudioListener and add it to the camera
const listener = new THREE.AudioListener();
camera.add(listener)
// create a global audio source
const audio = document.getElementById("audio");
const sound = new THREE.Audio( listener );
// load a sound and set it as the Audio object's buffer
const audioLoader = new THREE.AudioLoader();
analyser = new THREE.AudioAnalyser( sound, fft );
const play = (file) => {
audioLoader.load(file, buffer => {
sound.setBuffer( buffer );
sound.setLoop( true );
sound.setVolume( 0.8 );
sound.play();
})
isPlaying = true;
}
document.addEventListener("dragover", event => event.preventDefault());
document.addEventListener("drop", (event) => {
event.preventDefault();
if (sound.isPlaying) {
sound.stop();
}
play(event.dataTransfer.files[0].path);
})
fileButton.addEventListener("change", event => {
if (sound.isPlaying) {
sound.stop();
}
play(event.target.files[0].path);
})
playButton.addEventListener("click", event => {
if (sound.isPlaying) {
sound.stop();
}
sound.play();
})
pauseButton.addEventListener("click", event => {
sound.pause();
})