Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the loudness of HTML5 audio?

In an HTML5 game I'm making, I play a "thud" sound when things collide. However, it is a bit unrealistic. No matter the velocity of the objects, they will always make the same, relatively loud "thud" sound. What I'd like to do is to have that sound's loudness depend on velocity, but how do I do that? I only know how to play a sound.

playSound = function(id)
{
    sounds[id].play();
}

sounds is an array full of new Audio("url")'s.

like image 613
corazza Avatar asked Apr 09 '12 15:04

corazza


1 Answers

Use the audio element's volume property. From W3:

The element's effective media volume is volume, interpreted relative to the range 0.0 to 1.0, with 0.0 being silent, and 1.0 being the loudest setting, values in between increasing in loudness. The range need not be linear. The loudest setting may be lower than the system's loudest possible setting; for example the user could have set a maximum volume.

Ex: sounds[id].volume=.5;

like image 57
j08691 Avatar answered Sep 27 '22 16:09

j08691