Hello, is there a way playing sounds without loading the same sound every time? I mean when I play the same sound multiple times, it needs to load everytime. I tried to use asset loader, but it does not work or I don’t know how to use it.
What I’m trying to do - play a sound whenever I shoot, but it has a delay, because it needs to load (The shooting is pretty fast, so there can’t be just used sound.play(), the sound should play over already playing sounds)
it could look like this, everytime I shoot, the sound is delayed because it needs to load again and again… i need to have it loaded once and play it many times.
function shoot(){
const music = new BABYLON.Sound("weapon","https://cdn.glitch.com/a654e63b-496c-4135-a627-fb8e3b38591b%2Fweapon1.mp3",scene,null,{autoplay: true });
//bullet etc
}
let mouseDown = false;
document.onmousedown = function(){
if(mouseDown==false){
shoot();
mouseDown = true;
}
}
document.onmouseup = function(){
mouseDown = false
}
Hi,
For future references, what I would do is initiate the sound somewhere:
let sound = New BABYLON.Sound(“nameOfTheSound”, “URLOfTheSound”, scene…);
Then I would create a function that takes a sound as parameter:
function shoot(shootingSound){
shootingSound.stop();
shootingSound.play();
…
}
This way, whenever you press the shooting button, the previous shot-sound stops and the the sound plays again without creating a new Sound object, thus not taking long.