Attribut autoplay HTML
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
html
<tag autoplay></tag>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> :
html
<!DOCTYPE html>
<html>
<head>
<title>Title of the document</title>
</head>
<body>
<audio controls autoplay>
<source src="https://fr.w3docs.com/build/audios/jingle_bells.ogg" type="audio/ogg" />
<source src="https://fr.w3docs.com/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> :
html
<!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="https://fr.w3docs.com/build/videos/arcnet.io(7-sec).mp4" type="video/mp4" />
</video>
<p>Some information about video</p>
</body>
</html>Pratique
Quelle est la bonne utilisation de l'attribut 'autoplay' en HTML ?