AudioContext: phương thức createMediaStreamDestination()
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 createMediaStreamDestination() của giao diện AudioContext được dùng để tạo một đối tượng MediaStreamAudioDestinationNode mới gắn với một MediaStream WebRTC biểu diễn một luồng âm thanh, có thể được lưu vào tệp cục bộ hoặc gửi tới máy tính khác.
MediaStream được tạo ra khi nút được tạo và có thể truy cập thông qua thuộc tính stream của MediaStreamAudioDestinationNode. Luồng này có thể được dùng tương tự như một MediaStream nhận được qua navigator.getUserMedia. Ví dụ, nó có thể được gửi tới một peer ở xa bằng phương thức addStream() của RTCPeerConnection.
Để biết thêm chi tiết về các media stream destination node, hãy xem trang tham chiếu MediaStreamAudioDestinationNode.
Cú pháp
createMediaStreamDestination()
Tham số
Không có.
Giá trị trả về
Ví dụ
Trong ví dụ đơn giản sau, chúng ta tạo một MediaStreamAudioDestinationNode, một OscillatorNode và một MediaRecorder (vì vậy ví dụ hiện tại chỉ hoạt động trong Firefox và Chrome). MediaRecorder được thiết lập để ghi thông tin từ MediaStreamDestinationNode.
Khi nhấn nút, oscillator bắt đầu chạy và MediaRecorder được khởi động. Khi nút dừng được nhấn, cả oscillator lẫn MediaRecorder đều dừng. Việc dừng MediaRecorder khiến sự kiện dataavailable được kích hoạt, và dữ liệu sự kiện được đưa vào mảng chunks. Sau đó sự kiện stop được kích hoạt, một blob mới kiểu opus được tạo ra, chứa dữ liệu trong mảng chunks, rồi một cửa sổ (tab) mới được mở trỏ tới một URL được tạo từ blob.
Từ đây, bạn có thể phát và lưu tệp opus.
<button>Make sine wave</button> <audio controls></audio>
const b = document.querySelector("button");
let clicked = false;
const chunks = [];
const ac = new AudioContext();
const osc = ac.createOscillator();
const dest = ac.createMediaStreamDestination();
const mediaRecorder = new MediaRecorder(dest.stream);
osc.connect(dest);
b.addEventListener("click", (e) => {
if (!clicked) {
mediaRecorder.start();
osc.start(0);
e.target.textContent = "Stop recording";
clicked = true;
} else {
mediaRecorder.stop();
osc.stop(0);
e.target.disabled = true;
}
});
mediaRecorder.ondataavailable = (evt) => {
// Push each chunk (blobs) in an array
chunks.push(evt.data);
};
mediaRecorder.onstop = (evt) => {
// Make blob out of our blobs, and open it.
const blob = new Blob(chunks, { type: "audio/ogg; codecs=opus" });
document.querySelector("audio").src = URL.createObjectURL(blob);
};
Note: Bạn có thể xem ví dụ này trực tiếp, hoặc nghiên cứu mã nguồn trên GitHub.
Thông số kỹ thuật
| Specification |
|---|
| Web Audio API> # dom-audiocontext-createmediastreamdestination> |