From 9b0e20d82b2236ff21af6aeeff05038847e8af22 Mon Sep 17 00:00:00 2001 From: ericrius1 Date: Thu, 4 Feb 2016 17:57:25 -0800 Subject: [PATCH] stopping sound injector if sound was looping --- .../ACAudioSearchAndInject2.js | 27 ++++++++++++++----- .../acAudioSearchCompatibleEntitySpawner2.js | 2 +- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/examples/audioExamples/acAudioSearching/ACAudioSearchAndInject2.js b/examples/audioExamples/acAudioSearching/ACAudioSearchAndInject2.js index 0a251d1e5b..0ad0740310 100644 --- a/examples/audioExamples/acAudioSearching/ACAudioSearchAndInject2.js +++ b/examples/audioExamples/acAudioSearching/ACAudioSearchAndInject2.js @@ -19,7 +19,7 @@ var DEFAULT_SOUND_DATA = { }; var MIN_PLAY_INTERVAL = 0.2; -var UPDATE_TIME = 100; +var UPDATE_TIME = 2000; var EXPIRATION_TIME = 5000; var soundEntityMap = {}; @@ -54,17 +54,28 @@ function handleActiveSoundEntities() { if (soundProperties.timeWithoutAvatarInRange > EXPIRATION_TIME) { // An avatar hasn't been within range of this sound entity recently, so remove it from map print("NO AVATARS HAVE BEEN AROUND FOR A WHILE SO REMOVE THIS SOUND FROM SOUNDMAP!"); + // If the sound was looping, make sure to stop playing it + if (soundProperties.loop) { + print ("STOP SOUND INJECTOR!!"); + soundProperties.soundInjector.stop(); + } + + delete soundEntityMap[soundEntity]; print("NEW MAP " + JSON.stringify(soundEntityMap)); } else { // If this sound hasn't expired yet, we want to potentially play it! if (soundProperties.readyToPlay) { var newPosition = Entities.getEntityProperties(soundEntity, "position").position; - Audio.playSound(soundProperties.sound, { - volume: soundProperties.volume, - position: newPosition, - loop: soundProperties.loop - }); + if (!soundProperties.soundInjector) { + soundProperties.soundInjector = Audio.playSound(soundProperties.sound, { + volume: soundProperties.volume, + position: newPosition, + loop: soundProperties.loop + }); + } else { + soundProperties.soundInjector.restart(); + } soundProperties.readyToPlay = false; } else if (soundProperties.loop === false && soundProperties.interval !== -1) { // We need to check all of our entities that are not looping but have an interval associated with them @@ -82,6 +93,7 @@ function handleActiveSoundEntities() { } } + function handleFoundSoundEntities(entities) { entities.forEach(function(entity) { var soundData = getEntityCustomData(SOUND_DATA_KEY, entity); @@ -121,7 +133,8 @@ function handleFoundSoundEntities(entities) { soundEntityMap[entity] = soundProperties; } } else { - //If this sound is in our map already, we want to reset timeWithoutAvatarInRange + //If this sound is in our map already, we want to reset timeWithoutAvatarInRange + // Also we want to check to see if the entity has been updated with new sound data- if so we want to update! soundEntityMap[entity].timeWithoutAvatarInRange = 0; } } diff --git a/examples/audioExamples/acAudioSearching/acAudioSearchCompatibleEntitySpawner2.js b/examples/audioExamples/acAudioSearching/acAudioSearchCompatibleEntitySpawner2.js index dce1cda54f..4d9064c3a7 100644 --- a/examples/audioExamples/acAudioSearching/acAudioSearchCompatibleEntitySpawner2.js +++ b/examples/audioExamples/acAudioSearching/acAudioSearchCompatibleEntitySpawner2.js @@ -20,7 +20,7 @@ var userData = { soundKey: { url: "https://s3-us-west-1.amazonaws.com/hifi-content/eric/Sounds/dove.wav", volume: 0.3, - loop: false, + loop: true, interval: 2000, intervalSpread: 1000 }