【问题标题】:AudioContext is not setting the gain properly in JavaScriptAudioContext 没有在 JavaScript 中正确设置增益
【发布时间】:2021-03-10 07:49:18
【问题描述】:

按照这篇文章,我正在尝试将音频元素的音量设置为 1 以上。

https://cwestblog.com/2017/08/17/html5-getting-more-volume-from-the-web-audio-api/

我需要能够设置不止一次,所以我设置了一个全局数组来存储不同的结果,以便我可以根据参与者进行调整。

这似乎不起作用,对我来说,当增益设置时我无法分辨出任何区别,我做错了什么吗?

window.audioGainParticipants = [];

function amplifyMedia(participant_id, mediaElem, multiplier) {
  const exists = window.audioGainParticipants.find(
    (x) => x.participant_id === participant_id
  );

  let result = null;

  if (exists) {
    result = exists.result;
  } else {
    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);
    window.audioGainParticipants.push({ participant_id, result });
  }

  result.amplify(multiplier);
}

我这样称呼...

const audioElement = document.getElementById(
  `audio-${participantId}`
);

amplifyMedia(
  `audio-${participantId}`,
  audioElement,
  volume // number between 0-2
);

【问题讨论】:

    标签: javascript audiocontext


    【解决方案1】:

    那篇文章可能已经过时了。您不应该直接分配给增益的值,而是使用 setter 方法。

    setValueAtTime 非常简单,应该可以满足您的需求。文档显示了在增益节点上调用此方法的示例。 https://developer.mozilla.org/en-US/docs/Web/API/AudioParam/setValueAtTime

    还有setTargetAtTime,它稍微复杂一点,但如果您需要更改当前正在播放的内容的设置,听起来应该会更好。 https://developer.mozilla.org/en-US/docs/Web/API/AudioParam/setTargetAtTime

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多