Html: Add a volume level attribute to audio/video elements

Created on 24 Dec 2018  路  12Comments  路  Source: whatwg/html

For declaratively setting the initial/default volume as a fraction of maximum. Like

<audio src="lullaby.mp3" controls volume="0.5"></audio>

The attribute would reflect the volume IDL property of HTMLMediaElement.

additioproposal media

Most helpful comment

The other volume-related attribute, muted, reflects to defaultMuted rather than directly to muted. If we were to add a volume Attribute, it should probably reflect to defaultVolume, and have that value affect volume only until that property is set directly.

All 12 comments

cc @whatwg/media

The other volume-related attribute, muted, reflects to defaultMuted rather than directly to muted. If we were to add a volume Attribute, it should probably reflect to defaultVolume, and have that value affect volume only until that property is set directly.

Sure, I was debating myself about whether the attribute should reflect the live value or not, but went with the simpler proposal.

But, I suppose: using a separate defaultVolume IDL avoids any performance impacts from live-update of the attribute (e.g., if volume is being continuously animated), and means that the attribute doesn't get added to the element unless the author specifically put it there, so it doesn't change existing behavior.

I agree that reflection to defaultVolume would be better for the reasons given above. Though, I would like to point out that defaultMuted and muted have been very confusing to web developers and I quite often got bug reports from developers setting the muted content attribute after creating the element (which is ignored per spec). Other parts of HTML like input element accept changes of the default value until changes by script/user but the media element does not. If we want to add a volume content attribute we may want to consider following the media element (better consistency) or input element (matches developer's expectations) pattern.

Can you give an example for the input element case you mention? I thought it behaved rather similarly with checked/defaultChecked and such.

With input.value, you can do:

<html>
<input value=foo>
<script>
input.setAttribute('value', 'bar');
</script>
</html>

This will change the value to "bar".

With media.muted, if you do:

<html>
<video controls></video>
<script>
video.setAttribute('muted', '');
</script>
</html>

The video will not be muted.

I see, it only takes effect when the element is created, presumably because afterwards it could already be playing. (That does seem somewhat problematic, as it's a bit of an anti-pattern to check attributes at creation-time.)

It's it's quite an anti-pattern and Chrome used to not do it but IIRC we were the only browser to do this so we are now following the spec (which is why we got complaints). To recenter the discussion to the volume attribute, we would need to decide whether we want to:

  1. change muted spec and have volume follow muted
  2. do not change muted spec and have volume follow muted
  3. do not change muted spec and have volume follow usual pattern

I'd be very happy with a change here that makes the muted content attribute behave independently of the creation-time of the element. And I think I also agree that we should not introduce new attributes like that.

The most straightforward spec fix would be to change the muted content property / defaultMuted IDL property to match the checked content property / defaultChecked IDL property in HTMLInputElement, in that you can freely change the content property up until the non-default IDL property has been set. Then do the same for volume / defaultVolume.

FWIW, in Safari, the muted IDL property is fixed in place once the element has been inserted into the DOM, and not specifically at creation time. Supporting a defaultChecked style model would be very straightforward, as it means removing the "dirty mutedness" flag set during insertion.

Sounds great, so what's needed here is someone willing to do the work to change the standard around and someone willing to write tests for it.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dherman picture dherman  路  69Comments

jrlevine picture jrlevine  路  61Comments

dominiccooney picture dominiccooney  路  102Comments

smaug---- picture smaug----  路  89Comments

annevk picture annevk  路  60Comments