AudioContext: phương thức createMediaStreamSource()
Baseline
Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since April 2021.
Phương thức createMediaStreamSource() của giao diện AudioContext được dùng để tạo một đối tượng MediaStreamAudioSourceNode mới từ một media stream (ví dụ, từ một thể hiện MediaDevices.getUserMedia), âm thanh từ đó sau đó có thể được phát và xử lý.
Để biết thêm chi tiết về các media stream audio source node, hãy xem trang tham chiếu MediaStreamAudioSourceNode.
Cú pháp
createMediaStreamSource(stream)
Tham số
stream-
Một
MediaStreamđóng vai trò nguồn âm thanh để đưa vào đồ thị xử lý âm thanh cho việc sử dụng và thao tác.
Giá trị trả về
Một đối tượng MediaStreamAudioSourceNode mới biểu diễn nút âm thanh có phương tiện được lấy từ media stream nguồn đã chỉ định.
Ví dụ
Trong ví dụ này, chúng ta lấy một media stream (âm thanh + video) từ navigator.getUserMedia, đưa phương tiện vào một phần tử <video> để phát rồi tắt tiếng âm thanh, nhưng đồng thời cũng đưa âm thanh vào một MediaStreamAudioSourceNode.
Tiếp theo, chúng ta đưa nguồn âm thanh này vào một BiquadFilterNode low-pass (về cơ bản đóng vai trò như bộ tăng cường bass),
rồi đến AudioDestinationNode.
Thanh trượt phạm vi bên dưới phần tử <video> điều khiển lượng gain áp dụng cho bộ lọc low-pass. Tăng giá trị thanh trượt sẽ làm âm thanh nghe nặng bass hơn.
Note: Bạn có thể xem ví dụ này chạy trực tiếp, hoặc xem mã nguồn.
const pre = document.querySelector("pre");
const video = document.querySelector("video");
const myScript = document.querySelector("script");
const range = document.querySelector("input");
// getUserMedia block - grab stream
// put it into a MediaStreamAudioSourceNode
// also output the visuals into a video element
if (navigator.mediaDevices) {
console.log("getUserMedia supported.");
navigator.mediaDevices
.getUserMedia({ audio: true, video: true })
.then((stream) => {
video.srcObject = stream;
video.onloadedmetadata = (e) => {
video.play();
video.muted = true;
};
// Create a MediaStreamAudioSourceNode
// Feed the HTMLMediaElement into it
const audioCtx = new AudioContext();
const source = audioCtx.createMediaStreamSource(stream);
// Create a biquad filter
const biquadFilter = audioCtx.createBiquadFilter();
biquadFilter.type = "lowshelf";
biquadFilter.frequency.value = 1000;
biquadFilter.gain.value = range.value;
// connect the AudioBufferSourceNode to the gainNode
// and the gainNode to the destination, so we can play the
// music and adjust the volume using the mouse cursor
source.connect(biquadFilter);
biquadFilter.connect(audioCtx.destination);
// Get new mouse pointer coordinates when mouse is moved
// then set new gain value
range.oninput = () => {
biquadFilter.gain.value = range.value;
};
})
.catch((err) => {
console.log(`The following gUM error occurred: ${err}`);
});
} else {
console.log("getUserMedia not supported on your browser!");
}
// dump script to pre element
pre.textContent = myScript.textContent;
Note:
Do hệ quả của việc gọi createMediaStreamSource(), âm thanh từ media stream sẽ được định tuyến lại vào đồ thị xử lý của AudioContext. Vì vậy, bạn vẫn có thể phát/tạm dừng luồng thông qua API của phần tử phương tiện và các nút điều khiển trình phát.
Thông số kỹ thuật
| Thông số kỹ thuật |
|---|
| Web Audio API> # dom-audiocontext-createmediastreamsource> |