Skip to content
","url":"https://ez4code.com/snippets/html5-video-audio","keywords":"video, audio, media","author":{"@type":"Person","name":"EZ4Code Team"},"publisher":{"@type":"Organization","name":"EZ4Code","logo":{"@type":"ImageObject","url":"https://ez4code.com/logo.png"}},"datePublished":"2024-01-01","dateModified":"2026-08-01","image":"https://ez4code.com/og-image.png"}
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