Welcome to our comprehensive guide on HTML Audio DOM Methods! In this lesson, we'll delve into the world of audio in HTML, focusing on the various DOM methods that allow us to control and manipulate audio elements in our web projects.
DOM (Document Object Model) methods are functions that let us interact with the elements in our HTML document programmatically. In the context of audio, we have several useful methods that make it easy to control audio elements like the <audio> tag.
<audio> Tag π§Before we dive into the methods, let's quickly review the <audio> tag. This HTML tag allows you to embed audio files directly in your web pages, making it easy to create rich media experiences.
<audio src="myAudio.mp3" controls></audio>In the above example, myAudio.mp3 is the audio file we want to play, and controls enables the default HTML controls for playback.
play() βΆοΈThe play() method starts the audio playback from the current position.
const audio = document.querySelector('audio');
audio.play();pause() βΈοΈThe pause() method stops the currently playing audio.
const audio = document.querySelector('audio');
audio.pause();volume πThe volume property controls the audio's volume level between 0 (silent) and 1 (maximum volume).
const audio = document.querySelector('audio');
audio.volume = 0.5; // sets the volume to 50%currentTime β±οΈThe currentTime property gets or sets the current playback position in seconds.
const audio = document.querySelector('audio');
console.log(audio.currentTime); // shows the current playback position
audio.currentTime = 10; // sets the playback position to 10 secondsduration πThe duration property returns the total length of the audio file in seconds.
const audio = document.querySelector('audio');
console.log(audio.duration); // shows the total duration of the audiopaused βΈοΈThe paused property returns a boolean indicating whether the audio is currently paused (true) or playing (false).
const audio = document.querySelector('audio');
console.log(audio.paused); // shows whether the audio is paused or notWhich DOM method do we use to start the audio playback from the current position?
Now that you've learned the basic HTML Audio DOM methods, let's build a simple audio player that allows users to control the playback of a single audio file.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Simple Audio Player</title>
</head>
<body>
<audio id="myAudio" src="myAudio.mp3"></audio>
<br>
<button onclick="playAudio()">Play</button>
<button onclick="pauseAudio()">Pause</button>
<script>
const audio = document.getElementById('myAudio');
function playAudio() {
audio.play();
}
function pauseAudio() {
audio.pause();
}
</script>
</body>
</html>In this example, we have an <audio> tag with an id myAudio, two buttons for controlling playback, and a script that uses the play() and pause() methods to control the audio.
We've covered the essential HTML Audio DOM methods in this tutorial, allowing you to create interactive audio experiences in your web projects. With these methods, you can control the playback, volume, and even the current playback position of audio files using JavaScript. Happy coding! π