HTML5
Video & Audio
Embed media with multiple sources and control playback.
By EZ4Code Team
videoaudiomedia
Code
<video width="640" controls poster="cover.jpg">
<source src="movie.mp4" type="video/mp4">
<source src="movie.webm" type="video/webm">
Your browser does not support the video tag.
</video>
<audio controls>
<source src="song.mp3" type="audio/mpeg">
<source src="song.ogg" type="audio/ogg">
</audio>
<script>
const v = document.querySelector("video");
v.addEventListener("loadedmetadata", () => {
console.log("duration:", v.duration);
});
// play() / pause() control playback programmatically
// v.playbackRate = 2; // 2x speed
</script>Explanation
The video and audio elements embed media without plugins, with multiple source tags providing format fallbacks. The controls attribute adds a default UI, and the JS API exposes play(), pause(), currentTime, and playbackRate. Listening to events like loadedmetadata lets scripts react to playback state.
More HTML5 Snippets
Semantic Elements
Structure a page with header, nav, main, article, and footer.
Form Input Types
Use native HTML5 input types, validation, and datalist.
Canvas Basics
Draw shapes, lines, and text on a 2D canvas.
Local & Session Storage
Persist data in the browser with the Web Storage API.
Geolocation
Get the user's position and watch for changes.
Drag and Drop
Accept dropped files with the Drag and Drop API.