HTML Audio and Video
HTML Living Standard
Browsers can play media directly. Add controls so viewers can start and stop playback, and provide text for spoken and important non-speech audio.
On this page
Play audio with <audio>
Add controls to show the browser’s audio player. With source elements, the browser tries available files in order. A type can let it skip formats it knows it cannot play. Keep a text transcript near the player so people can read the same information.
How to try it In index.html, replace the content between <body> and </body> with this example. Save the file and reload it in the browser.
<audio controls>
<source src="https://javascript-training.cloudninemm.com/media/lesson.ogg" type="audio/ogg">
Your browser does not support this audio player.
</audio>
<p>Sample audio: a short tone.</p>
What to expect With an internet connection, the player plays a short tone when you press Play.
Play video with <video>
A video element displays moving pictures and can play audio. controls shows the browser’s buttons and timeline. preload="metadata" hints that the browser can read the duration first. If you have a separate cover image, add its URL in poster to display it before playback.
How to try it In index.html, replace the content between <body> and </body> with this example. Save the file and reload it in the browser.
<video controls preload="metadata" width="360">
<source src="https://javascript-training.cloudninemm.com/media/lesson.webm" type="video/webm">
Your browser does not support this video player.
</video>
What to expect With an internet connection, use Play to see a short video with moving color bars.
Timed captions with <track>
A track uses a WebVTT file containing timestamps and the text to display. A captions track includes speech and relevant sounds. srclang tells the browser the language, label names it in the player, and default requests that the track be enabled initially if the user’s preferences do not override it. Serve the HTML, video, and VTT from the same local web server when trying the sample.
How to try it Download lesson.webm and lesson-en.vtt from https://javascript-training.cloudninemm.com/media/lesson.webm and https://javascript-training.cloudninemm.com/media/lesson-en.vtt. Put them beside index.html and place this HTML in body. Serve the folder using Live Server or python -m http.server 8000 and open http://localhost:8000.
<video controls src="lesson.webm">
<track kind="captions" src="lesson-en.vtt" srclang="en" label="English" default>
</video>
What to expect Play the video or select English in the player’s caption menu. The browser loads lesson-en.vtt from the URL you provided.
Write a WebVTT cue
Save this example as lesson-en.vtt beside the video. WEBVTT identifies the format. A cue starts with a time range and is followed by the text that should appear. Separate successive cues with a blank line.
How to try it Replace the contents of lesson-en.vtt with the code below, save it, and reload the page on the same local server.
WEBVTT
00:00:00.000 --> 00:00:03.000
[Color bars move across the screen.]
What to expect With captions enabled, the new cue displays from 0 to 3 seconds. It describes what happens in this sample clip.