Skip to content

Instantly share code, notes, and snippets.

@westc
Last active January 8, 2023 22:18
Show Gist options
  • Save westc/e70bca9c8223d89e855f655e4263b3bb to your computer and use it in GitHub Desktop.
Save westc/e70bca9c8223d89e855f655e4263b3bb to your computer and use it in GitHub Desktop.
Function that can be used to amplify the volume of a media element (<audio> or <video>).
function amplifyMedia(mediaElem, multiplier) {
var context = new (window.AudioContext || window.webkitAudioContext),
result = {
context: context,
source: context.createMediaElementSource(mediaElem),
gain: context.createGain(),
media: mediaElem,
amplify: function(multiplier) { result.gain.gain.value = multiplier; },
getAmpLevel: function() { return result.gain.gain.value; }
};
result.source.connect(result.gain);
result.gain.connect(context.destination);
result.amplify(multiplier);
return result;
}
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>My Test Video</title>
</head>
<body>
<video id="myVideo" controls src="video.mp4"></video>
<script src="createAmplifier.js"></script>
<script type="text/JavaScript">
// Make my video twice as loud as normal.
createAmplifier(document.getElementById('myVideo'), 2);
</script>
</body>
</html>
@rogerhub
Copy link

This is really useful. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment