BaseAudioContext: createGain() 方法
BaseAudioContext 介面的 createGain() 方法建立一個 GainNode,該節點可用於控制整個音訊圖的增益(或音量)。
注意: 建立 GainNode 的推薦方式是使用 GainNode() 建構函式;請參閱 建立 AudioNode。
語法
js
createGain()
引數
無。
返回值
一個 GainNode,它接收一個或多個音訊源作為輸入,並輸出音量已根據節點的 GainNode.gain a-rate 引數調整到指定級別的音訊。
示例
下面的示例展示瞭如何使用 AudioContext 建立一個 GainNode,然後透過更改 gain 屬性值來靜音和取消靜音音訊(在使用者點選“靜音”按鈕時)。
下面的程式碼片段無法獨立執行——有關完整的可執行示例,請檢視我們的 Voice-change-O-matic 演示 (檢視原始碼)。
html
<div>
<button class="mute">Mute button</button>
</div>
js
const audioCtx = new AudioContext();
const gainNode = audioCtx.createGain();
const mute = document.querySelector(".mute");
let source;
if (navigator.mediaDevices.getUserMedia) {
navigator.mediaDevices.getUserMedia(
// constraints - only audio needed for this app
{
audio: true,
},
// Success callback
(stream) => {
source = audioCtx.createMediaStreamSource(stream);
},
// Error callback
(err) => {
console.error(`The following gUM error occurred: ${err}`);
},
);
} else {
console.error("getUserMedia not supported on your browser!");
}
source.connect(gainNode);
gainNode.connect(audioCtx.destination);
// …
mute.onclick = () => {
if (mute.id === "") {
// 0 means mute. If you still hear something, make sure you haven't
// connected your source into the output in addition to using the GainNode.
gainNode.gain.setValueAtTime(0, audioCtx.currentTime);
mute.id = "activated";
mute.textContent = "Unmute";
} else {
gainNode.gain.setValueAtTime(1, audioCtx.currentTime);
mute.id = "";
mute.textContent = "Mute";
}
};
規範
| 規範 |
|---|
| Web Audio API # dom-baseaudiocontext-creategain |
瀏覽器相容性
載入中…