HTML Audio and Video DOM Reference
Learn HTML through interactive, bite-sized lessons. Build real websites and see your code come to life instantly.
Start HTML Journey →The HTML Audio and Video DOM Reference provides a set of properties and methods for manipulating audio and video elements in HTML5 using JavaScript. This powerful feature allows developers to create interactive media experiences on web pages.
Audio and Video Elements
HTML5 introduced the <audio> and <video> elements, which are part of the HTML5 Media specification. These elements enable native multimedia playback without the need for plugins.
Common Properties
Both audio and video elements share several properties that can be accessed and modified through JavaScript:
currentTime: Get or set the current playback position in secondsduration: Read-only property that returns the length of the media in secondspaused: Boolean indicating whether the media is pausedvolume: Get or set the volume level (0.0 to 1.0)
Common Methods
JavaScript provides methods to control media playback:
play(): Start playing the mediapause(): Pause the mediaload(): Reload the media element
Example: Controlling Audio Playback
Here's a simple example demonstrating how to control an audio element using JavaScript:
<audio id="myAudio" src="audio.mp3"></audio>
<button onclick="playAudio()">Play</button>
<button onclick="pauseAudio()">Pause</button>
<script>
function playAudio() {
document.getElementById("myAudio").play();
}
function pauseAudio() {
document.getElementById("myAudio").pause();
}
</script>
Video-Specific Properties
Video elements have additional properties:
videoWidthandvideoHeight: Read-only properties returning the intrinsic dimensions of the videoposter: Get or set the URL of an image to display before the video plays
Example: Manipulating Video
This example shows how to work with video properties and methods:
<video id="myVideo" src="video.mp4" poster="thumbnail.jpg"></video>
<button onclick="togglePlay()">Play/Pause</button>
<script>
function togglePlay() {
var video = document.getElementById("myVideo");
if (video.paused) {
video.play();
} else {
video.pause();
}
}
// Get video dimensions
var videoWidth = document.getElementById("myVideo").videoWidth;
var videoHeight = document.getElementById("myVideo").videoHeight;
console.log("Video dimensions: " + videoWidth + "x" + videoHeight);
</script>
Event Handling
Audio and video elements emit various events that can be listened to:
play: Fired when playback startspause: Fired when playback is pausedended: Fired when playback reaches the endtimeupdate: Fired when the current playback position changes
These events allow developers to create custom controls and synchronize other elements with media playback.
Best Practices
- Always provide fallback content for browsers that don't support HTML5 audio or video.
- Use the
preloadattribute to control how much of the media is loaded when the page loads. - Implement custom controls for better accessibility and cross-browser consistency.
- Consider using the HTML Picture Element for responsive video posters.
By mastering the HTML Audio and Video DOM Reference, developers can create rich, interactive media experiences that enhance user engagement and accessibility on the web.