Skip to content

Instantly share code, notes, and snippets.

@amundo
Forked from westc/createAmplifier.js
Created January 23, 2018 01:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save amundo/c9f329da840680defb5b775a390cc19c to your computer and use it in GitHub Desktop.
Save amundo/c9f329da840680defb5b775a390cc19c 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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment