Ambience sound player with random time


#1

Save script as RiRa_PlayAmbiancesoundRandom.js.
Change sound url when needed, it’s now set to bird sound. save script.
Upload it to ATP as example.
Put ATP url in entity cube. And have fun.

//----------------------------
//
//  RiRa_PlayAmbiancesoundRandom.js
//
//  Created by Richardus Raymaker. 17-06-2016
//
//  Creative Common Attribution 4.0 International (CC BY 4.0)
//  http://creativecommons.org/licenses/by/4.0/
//
//    soundfile,  
//    https://www.freesound.org/people/tdes/sounds/155767/
//    http://creativecommons.org/licenses/by-nc/3.0/
//
//----------------------------

(function()
{ 
    var soundurl = SoundCache.getSound("http://market.simsquaremetaverse.nl/sounds/ambiance/181574__mings__tui-new-zealand-bird.wav");
    var soundvolume = 1.0;

    var Ent;
    var timer;
    this.preload=function(entityID)
    { 
        Ent = entityID;
        timer=Script.setInterval(play, getRandomInt(10000,60000));
    }
    
    function getRandomInt(min, max) 
    {
        return Math.floor(Math.random() * (max - min)) + min;
    }    
    
    function play()
    {     
        var copyEnt = Entities.getEntityProperties(Ent, ["position"]);
        var jsonEnt = JSON.stringify(copyEnt);
        var data = JSON.parse(jsonEnt);    
        var entitypos = data.position;
        
        Audio.playSound(soundurl, { loop: false, position: entitypos ,volume: soundvolume });
        Script.clearInterval(timer);    
        timer=Script.setInterval(play, getRandomInt(15000,60000));
    }    
}
)