var soundMap = [{ name: 'Cells', url: "http://hifi-content.s3.amazonaws.com/james/debug/sound/Cells_mono.wav", audioOptions: { position: { x: 1000, y: 1000, z: 1000 }, volume: 1, loop: true } } ]; function loadSounds() { soundMap.forEach(function(soundData) { soundData.sound = SoundCache.getSound(soundData.url); }); } function playSound(soundData) { if (soundData.injector) { // try/catch in case the injector QObject has been deleted already try { soundData.injector.stop(); } catch (e) { print('error playing sound' + e) } } soundData.injector = Audio.playSound(soundData.sound, soundData.audioOptions); } function checkDownloaded(soundData) { if (soundData.sound.downloaded) { Script.clearInterval(soundData.downloadTimer); if (soundData.hasOwnProperty('playAtInterval')) { soundData.playingInterval = Script.setInterval(function() { playSound(soundData) }, soundData.playAtInterval); } else { playSound(soundData); } } } function startCheckDownloadedTimers() { soundMap.forEach(function(soundData) { soundData.downloadTimer = Script.setInterval(function() { checkDownloaded(soundData); }, 1000); }); } Script.scriptEnding.connect(function() { soundMap.forEach(function(soundData) { if (soundData.hasOwnProperty("injector")) { soundData.injector.stop(); } if (soundData.hasOwnProperty("downloadTimer")) { Script.clearInterval(soundData.downloadTimer); } if (soundData.hasOwnProperty("playingInterval")) { Script.clearInterval(soundData.playingInterval); } }); }); loadSounds(); startCheckDownloadedTimers();