Can I have multiple src attributes in an HTML5 <audio> element? Or the src attribute and the <source> element?

I was wondering what this means that the <audio> element can have an src attribute or one or more <source> elements for its content.

  • Can I have two or more src attributes? If so, how?
  • Can I have an src attribute and a <source> element?

Here is a link to what I'm talking about: http://dev.w3.org/html5/markup/audio.html#audio

Can someone explain this to me in detail?

+4
source share
3 answers

These questions in HTML5 always consist of two parts: is this really the case, and what happens if I do this?

1. Is it really?

No. Although the link to the markup language link does not seem useful, as it omits a critical fact. (see below).

Instead, view the actual specification for the audio element .

The content model for this says:

If an element has the src attribute: zero or more track elements, then transparent, but with no descendants of media elements.

If an element does not have the src attribute: one or more sources of elements, then zero or more track elements, then transparent, but with no descendants of media elements.

Thus, the source element is not specifically invoked as resolved when the src attribute is present.

But what about a transparent bit? This means that elements that are valid inside the parent element of the audio element are also valid inside the audio element. Therefore, if the source element was valid in the parent element, then it will also be valid inside the audio element, even if the audio element has the src attribute.

Is it possible? If we check the specification for the source element , we will see that the context in which this element can be used is

Being a child of a media element, before any stream content or track elements.

Thus, the parent element of the audio element must be a multimedia element, that is, <video> or <audio> .

However, again, when defining the content model of an audio element (see above), it says that audio cannot have descendants of media elements - therefore, an audio element cannot be embedded in another audio element. The video element is defined in a similar way, so <source> cannot be a valid child <audio> element through the "transparent" part of the audio element content model.

Therefore, a sound element with the src and <source> attributes cannot be valid.

(Note that the link to the Markup Language link that you are linking to does not mention the restriction of descending descendants of media elements, so I don’t believe that you can determine the correct check from it.)


2. What happens if I do this?

This is a bit easier. The specification for the src attribute for media elements says:

There are two ways to specify a media resource, an src attribute, or a source element. The attribute overrides elements.

In other words, if the src attribute is present, any <source> elements are simply ignored.


See Ian Boyd's answer for a few src attributes.

+4
source

You are asking:

 <AUDIO src="file1.wav" src="file2.wav" src="file3.wav"></AUDIO> 

really?

I will say no - this does not make sense:

  • you cannot specify an attribute more than once
  • which file do you want to play
+1
source

You can very well have several source elements, for example:

 <audio> <source src="music.mp3" type="audio/mpeg" /> <source src="music.ogg" type="audio/ogg" /> </audio> 

UAs will play the first file they reach, which they can play.

If you also specify the src attribute, then this takes precedence over others.

You cannot do

 <audio> <source src="test.mp3" type="audio/mpeg" /> <source src="test2.mp3" type="audio/mpeg" /> </audio> 

although they expect that the game, only the first will be.

+1
source

Source: https://habr.com/ru/post/1346766/


All Articles