Get and Set Volume with JavaScript

By  on  

The <audio> and <video> tags provide a wealth more functionality than most people know. For instance, did you know that you could detect supported video formats and audio formats using a few JavaScript tricks?  It got me to thinking about the possibilities of detecting system volume with JavaScript in the browser.

I hate to be a buzzkill but unfortunately JavaScript doesn't provide direct access to the system volume but you can, using <audio> and/or <video> elements, programmatically set and get the volume level.

// Getting volume level
const volume = document.querySelector("video").volume; // 1 

// Setting volume level
document.querySelector("video").volume = 0.5;  // set volume to 50%

You can also listen for volume changes with the "onvolumechange" event:

document.querySelector("video").addEventListener("onvolumechange", e => {
    // Change your custom control UI
});

It makes sense that you can't set system volume level from a random JavaScript snippet in a browser but I had a slight hope you could retrieve that level.  Setting volume with JavaScript for a given piece of media is relative to system volume level but hey -- at least we get to create custom controls for those elements with .volume settings!

Recent Features

  • By
    7 Essential JavaScript Functions

    I remember the early days of JavaScript where you needed a simple function for just about everything because the browser vendors implemented features differently, and not just edge features, basic features, like addEventListener and attachEvent.  Times have changed but there are still a few functions each developer should...

  • By
    Camera and Video Control with HTML5

    Client-side APIs on mobile and desktop devices are quickly providing the same APIs.  Of course our mobile devices got access to some of these APIs first, but those APIs are slowly making their way to the desktop.  One of those APIs is the getUserMedia API...

Incredible Demos

  • By
    Xbox Live Gamer API

    My sharpshooter status aside, I've always been surprised upset that Microsoft has never provided an API for the vast amount of information about users, the games they play, and statistics within the games. Namely, I'd like to publicly shame every n00b I've baptized with my...

  • By
    Color Palette Generator Using jQuery

    As I continue to learn jQuery, I think it's important that I begin by porting over scripts I've created using MooTools. One of those scripts is my Color Palette Generator script, which debuted on Eric Wendelin's blog. For those of you that...

Discussion

  1. Great article! I would be curious on if this would be possible in node.js since it does have access to system files

  2. Tim

    Except on iOS, where the volume has always been read only. Apparently Apple didn’t want applications to have access to the volume knob even though the system volume is under user control via hardware.

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!