is it possible to make only one volume control for multiple audio effects?

Sure, you have to loop throught each audio elements. See comments in the code below.

let volumeControl = document.getElementById('vol-control');

function setVolume (){

  console.clear()
  console.log(volumeControl.value)
  
  // Get the array of audio element and loop through them to set the new volume value
  Array.from(document.querySelectorAll("audio")).forEach(function(audio){
  
      //  if the input value is "", use zero
      audio.volume = volumeControl.value == "" ? 0 : volumeControl.value / 100;
    })
};

volumeControl.addEventListener('change', setVolume);
volumeControl.addEventListener('input', setVolume);

// On load
setVolume()
<input id="vol-control" type="number" min="0" max="100" value="25">
<br>


<audio src="https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3" id="bgsound1" controls></audio>
<audio src="https://www.soundhelix.com/examples/mp3/SoundHelix-Song-8.mp3" id="bgsound2" controls></audio>
<audio src="https://www.soundhelix.com/examples/mp3/SoundHelix-Song-16.mp3" id="bgsound3" controls></audio>

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top