【发布时间】:2020-10-25 21:26:50
【问题描述】:
我的目标是什么?
控制每个音轨的音量,这样我就可以在较低的音量下播放背景声音,在较高的音量下播放其他声音。
场景
- 我有一个上传音频文件的页面,上传完成后会在 DOM 上创建一个音频播放器。
- 音频源连接到 audioContext.destination 和 mediaStreamDestination。
- 音频流工作正常。
我为每个音频播放器添加了一个音量范围,我正在尝试控制音量。
volumeControlInput.addEventListener("change", (event) => {
const audioSource = audioSources[index];
const gainNode = audioContext.createGain();
// Connect source to a gain node
audioSource.connect(gainNode);
// Connect gain node to destination
gainNode.connect(audioContext.destination);
gainNode.connect(mediaStreamDestination); //this doesn't seem necessary but I tried with and without it and it doesn't change anything.
const volume = event.target.value;
const fraction = parseInt(volume) / parseInt(event.target.max); // max is 100
// Using an x*x curve (x-squared) since simple linear (x) does not
// sound as good.
// I also tried setValueAtTime and there's no difference for me.
gainNode.gain.value = fraction * fraction;
}
我要解决的问题是什么?
如果我拖动音量范围输入,音量只会增加,它永远不会降低音量。
对我来说奇怪的是,这个例子在这里很好用 https://webaudioapi.com/samples/volume/ 来源:https://github.com/borismus/webaudioapi.com/blob/master/content/posts/volume/volume-sample.js
我想知道这是否根本不可能,有什么想法吗?
【问题讨论】:
标签: javascript webrtc web-audio-api