Do you have to use a seperate tag for Audio Source? Can you use it like an img tag?

Why do you have to use a separate tag for audio? Is there a reason, why you can’t just add the src attribute directly to the audio tag, like you do with img: .

5 Likes

You don’t have to do it the way they showed it in the lesson, can can use the <audio> tag in the same way as the <video> tag.

Like:

<audio 
    controls 
    src="/media/examples/t-rex-roar.mp3">Your browser does not support the 
    <code>audio</code> element.
</audio>

One thing to take into account is that not all browsers support the same file types, so you might want to use multiple sources of different types of audio file, in that case, you will want to do it like they did it in the lesson:

<audio controls>
  <source src="myAudio.mp3" type="audio/mpeg">
  <source src="myAudio.ogg" type="audio/ogg">
  <p>Your browser doesn't support HTML5 audio. Here is
     a <a href="myAudio.mp4">link to the audio</a> instead.</p>
</audio>

Take a look at the example shown here: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/audio#Attributes

11 Likes

If audio tag and video tag works well with a nested source tag, so it could work well in all media tags like img tag and so on?

Lesson: https://www.codecademy.com/courses/learn-html/lessons/semantic-html/exercises/audio-and-attributes

Great day.

what if i used the src attribute in the audio element? like we do with the img tag ? is it possible ? is it necessary to use the source tag ?

Yes, I am pretty sure that is possible since the program didn’t register that as an error, but also try looking that question up. Hope this helped!

Very useful, thanks!