HTML audio Play music stream / sound file / audio effect file / audio file

 The <audio> tag (tag) is used to play multimedia sound content, such as playing MP3 music.

grammar:

<audio src="example.mp3" autoplay controls></audio>

<audio> tag attributes (attributes):

  • src: The address of the audio resource (URL)
  • preload: Prompt the browser whether to preload resources. These values ​​can be used:
    • none: Do not preload, because the user may not play the audio, or you want to save more server bandwidth
    • metadata: The user may not play the audio, but it is necessary to download the metadata of the audio first
    • auto: The user is likely to play the audio, you can download it first
  • autoplay: Boolean attribute, which controls whether to play automatically, the default is false
  • loop: The boolean attribute, which controls whether to play repeatedly, the default is false
  • muted: Boolean attribute, which controls whether to mute or not, the default is false
  • controls: Boolean (boolean) attribute, specify whether to display the audio control panel, provided by the browser, there will be playback progress, pause button, play button, mute button, etc. The default is false

<audio> sound source addresses except srcthe specified property can also be used in the <audio> tag inside <source>the label set, you can use multiple <source> to specify a different format types of audio sources, and the browser they will have first pick Supported format to load.

The attributes of the <source> tag:

  • src: Audio address (URL)
  • type: The MIME type of the audio

Usage example:

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

<source> is an empty element without closing tag.


In <audio>, the content other than <source> will be regarded as fallback content, which will be displayed to the browser when the resource fails to load or the <audio> tag is not supported.

<audio controls>
  <source src="music.ogg" type="audio/ogg">
  <source src="music.mp3" type="audio/mpeg">
  Your browser does not support audio tag!
</audio>

Post a Comment

0 Comments