The HTML autoplay attribute enables audio or video elements to automatically start playing audio or video as soon as the page loads, providing uninterrupted playback.
Definition and Usage
➔ The autoplay attribute is a boolean attribute.
➔ Users may not like this, so it should be avoided if possible.
➔ If you must offer autoplay functionality, it should be opt-in for users so they can enable it or not.
Example
<audio controls autoplay>
<source src="audio-source-name1.mp3" type="audio/mpeg">
<source src="audio-source-name2.ogg" type="audio/ogg">
</audio>
<p>Note: Your browser may not support the audio element.</p>
Example
<video controls autoplay>
<source src="video-source-name1.mp4" type="video/mp4">
<source src="video-source-name2.ogg" type="video/ogg">
</video>
<p>Note: Your browser may not support the video element.</p>
Example
<!DOCTYPE html>
<html>
<head>
<title>HTML autoplay Attribute Example</title>
<meta charset="utf-8" />
</head>
<body>
<h2>HTML autoplay Attribute Example</h2>
<p>If there is a problem, check the location of the source file and the compatibility of browsers.</p>
<video controls autoplay>
<source src="video-source-name1.mp4" type="video/mp4">
<source src="video-source-name2.ogg" type="video/ogg">
</video>
<p>Note: Your browser may not support the video element.</p>
</body>
</html>
Click on the "copy" button and try this example in your project.