Volume Controller with HTML, CSS, and JavaScript

Kunal Kumar
3 min readJun 13, 2023
Photo by Jexo on Unsplash

Introduction:

Welcome, we’ll explore the art of creating a customizable volume controller using the dynamic trio of web technologies: HTML, CSS, and JavaScript. In this tutorial, we’ll guide you through the process of building a sleek and interactive volume control that you can easily integrate into your web projects.

Setting Up the HTML and CSS Structure:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<style>
input {
height: 1px;
}
</style>
</head>
<body>
<div>
<p>Slide To Adjust Volume</p>
<input type="range" id="volumecontroller" min="0" max="100" value="10" />
</div>
</body>
<script src="script.js"></script>
</html>

Within the <div> container, we have a <p> element displaying the message "Slide To Adjust Volume." This text serves as an instruction for users to interact with the volume controller. Following the text, we find an <input> element with the type set to "range." This input element provides a slider that users can slide horizontally to adjust the volume. The id attribute is set as "volumecontroller," and we've set the minimum value to 0, the maximum value to 100, and the initial value to 10.
Also we have used inline style to adjust height of <input> element.

Implementing Volume Control Logic:


const volumeController = document.getElementById("volumecontroller")
volumeController.addEventListener("input", () => {
HTMLMediaElement.volume=volumeController.value/100
})
  1. Understanding HTMLMediaElement.volume: In the given code, we encounter the HTMLMediaElement.volume property. The HTMLMediaElement interface represents a media-related element in the Document Object Model (DOM), such as an <audio> or <video> element. The volume property allows us to adjust the audio volume programmatically. By assigning a value between 0 and 1 to this property, we can control the audio output level. A value of 0 mutes the audio, while 1 represents the maximum volume.
  2. Range of Media Volume: The volume control range typically spans from 0 to 1, as defined by the HTMLMediaElement.volume property. However, for user convenience, volume controllers often use a more user-friendly range, such as 0 to 100. This broader range allows for finer-grained adjustments. In the provided code, the volumeController input element has a range of 0 to 100, making it easier for users to interact with the volume control.
  3. Dividing by 100: As mentioned, the volumeController input element has a range of 0 to 100. However, the HTMLMediaElement.volume property operates on a scale of 0 to 1. To bridge this gap between the user-friendly range and the underlying property range, we need to map the user input to the corresponding value within the range of 0 to 1. By dividing the volumeController.value by 100, we convert the value to a decimal representation that aligns with the HTMLMediaElement.volume property.
  4. Implementing the Event Listener: The provided code snippet adds an event listener to the volumeController input element, listening for the “input” event. Whenever the user interacts with the volume controller by sliding it, this event fires. The callback function updates the HTMLMediaElement.volume property with the adjusted volume level based on the value of volumeController.value divided by 100. This ensures that the volume control accurately reflects the user’s desired audio level.

By understanding the HTMLMediaElement.volume property and the range of media volume, you can now confidently implement and customize volume controllers in your web projects. Remember to divide the user input by 100 to map the desired volume to the HTMLMediaElement.volume property’s range of 0 to 1. This empowers your users to fine-tune their audio experience seamlessly. So go ahead, harness the power of audio control, and elevate your web applications to new heights!

--

--

Kunal Kumar

Vanilla JavaScript Perfection | MERN Stack Developer | DSA in C++ | Aspiring DevOps Engineer