Last active
January 8, 2023 22:18
-
-
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>).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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
This is really useful. Thanks!