W3docs

Attribut autoplay HTML

The HTML autoplay specifies that the audio or video will start playing automatically as soon as possible. See how to use it on the <audio> and <video> elements.

L'attribut autoplay de HTML est un attribut booléen qui spécifie que l'audio ou la vidéo commencera à se lire automatiquement dès que possible.

Vous pouvez utiliser cet attribut sur les éléments suivants : <audio> et <video>.

Syntaxe

<tag autoplay>&lt;/tag&gt;

Note : Les navigateurs modernes restreignent l'autoplay pour les médias contenant une piste audio. Pour garantir que l'autoplay fonctionne, vous devez également ajouter l'attribut muted à l'élément.

Exemple d'utilisation de l'attribut autoplay HTML sur l'élément <audio> :

<!DOCTYPE html>
<html>
  <head>
    <title>Title of the document</title>
  </head>
  <body>
    <audio controls autoplay>
      <source src="/build/audios/jingle_bells.ogg" type="audio/ogg" />
      <source src="/build/audios/audio.mp3" type="audio/mpeg" />
    </audio>
    <p>Click the play button</p>
  </body>
</html>

Exemple d'utilisation de l'attribut autoplay HTML sur l'élément <video> :

<!DOCTYPE html>
<html>
  <head>
    <title>Title of the document</title>
  </head>
  <body>
    <video width="320" height="240" controls autoplay muted>
      <source src="http://techslides.com/demos/sample-videos/small.ogv" type="video/ogg" />
      <source src="/build/videos/arcnet.io(7-sec).mp4" type="video/mp4" />
    </video>
    <p>Some information about video</p>
  </body>
</html>

Pratique

Pratique

Quelle est la bonne utilisation de l'attribut 'autoplay' en HTML ?