朋也的博客 » 首页 » 文章

HTML Video/Audio 音量提升超过1.0

作者:朋也
日期:2021-05-10
类别:javascript学习笔记 


版权声明:自由转载-非商用-非衍生-保持署名(创意共享3.0许可证

有时候看一些网页上的视频音量调到100%,还是听着很小,尝试着用js调用video的api来调调整视频播放器的音量,发现没法超过1.0

随后在sof上找到了一篇文章,可以解决音量上限的问题,备份一下

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>