-
Notifications
You must be signed in to change notification settings - Fork 3
/
PreloadNativeSounds.html
33 lines (32 loc) · 1.77 KB
/
PreloadNativeSounds.html
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
<!DOCTYPE html>
<title>PreloadNativeSounds Behavior Example</title>
<script src="https://cdn.jsdelivr.net/combine/npm/three@0.95.0/build/three.min.js,npm/altspace@2.9.0/dist/altspace.min.js,npm/altspacevr-behaviors@1.1.5/js/altspaceutil.min.js"></script>
<script>
altspaceutil.getFullspaceApp().then(app => {
// Preload sounds for caching purposes only, underlying sound objects will be deleted after being cached.
let cube = new THREE.Mesh(new THREE.BoxGeometry(100, 100, 100), new THREE.MeshBasicMaterial());
app.anchor.add(cube);
app.anchor.addBehavior(new altspaceutil.behaviors.PreloadNativeSounds(['test.mp3', 'test.ogg']));
app.anchor.addEventListener('n-sound-preloaded', event => {
cube.addEventListener('cursordown', event => {
let soundcomponent = cube.getBehaviorByType('n-sound');
if(soundcomponent) cube.removeBehavior(soundcomponent);
cube.addBehavior(new altspaceutil.behaviors.NativeComponent('n-sound', { src: /Mobile/i.test(navigator.userAgent) ? 'test.mp3' : 'test.ogg', oneshot: true, autoplay: true }, { useCollider: true }));
});
});
// Preload sounds and save sound objects for later use.
let soundlibrary = new THREE.Group();
app.anchor.add(soundlibrary);
let nsounddata = [
{ src: 'soundtest1.wav', oneshot: true, autoplay: false, volume: 0.5 },
{ src: 'soundtest2.wav', oneshot: true, autoplay: false, volume: 0.8 }
];
soundlibrary.addBehavior(new altspaceutil.behaviors.PreloadNativeSounds(nsounddata, { dispose: false, timeout: 15000 }));
soundlibrary.addEventListener('n-sound-preloaded', event => {
for(let sound of event.sounds) {
let soundcomponent = sound.getBehaviorByType('n-sound');
if(!event.timeout || soundcomponent.getAttribute('loaded')) soundcomponent.callComponentAction('play');
}
});
});
</script>