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

without auto control your video will not play until you hit the play button. Its for good user experience. for example: When you open youtube and scroll it down so the videos will start playing automatically without opening it.

It will display the video but It will not play until you gives it its control by attribute control. Without control it is just like a picture that we can only see like a thumbnail but cant play it.

Please i would like to ask whether its necessary to add src to Source when trying to code an audio file, cant we just use src without adding Source as src is already enclosed in Audio Tag??

1 Like

Using the src Attribute with the <audio> Tag

<audio controls src="audiofile.mp3"></audio>

Using the src attribute with multiple sources

<audio controls>
  <source src="audiofile.mp3" type="audio/mpeg">
  <source src="audiofile.ogg" type="audio/ogg">
  Your browser does not support the audio element.
</audio>

Differences and Use Cases

  1. Using <source> Elements:
  • Multiple Sources: If you want to provide multiple audio formats to ensure compatibility with different browsers, using the <source> elements is preferred.
  • Fallback Content: You can include fallback content inside the <audio> tag if none of the specified sources are supported.
  • Type Specification: You can specify the type of each source, which can help the browser quickly determine if it can play the file.
  1. Using the src Attribute:
  • Simpler Code: This method is simpler and shorter, suitable when you have only one audio format to support.
  • No Type Specification: You cannot specify the type attribute with this method.
2 Likes