AudioContext: sự kiện sinkchange
Limited availability
This feature is not Baseline because it does not work in some of the most widely-used browsers.
Experimental: This is an experimental technology
Check the Browser compatibility table carefully before using this in production.
Sự kiện sinkchange của giao diện AudioContext được kích hoạt khi thiết bị đầu ra âm thanh (và do đó là AudioContext.sinkId) đã thay đổi.
Cú pháp
Dùng tên sự kiện trong các phương thức như addEventListener(), hoặc đặt một thuộc tính xử lý sự kiện.
addEventListener("sinkchange", (event) => { })
onsinkchange = (event) => { }
Kiểu sự kiện
Ví dụ
Một trình nghe sự kiện sinkchange có thể được dùng để báo cáo việc thay đổi thiết bị đầu ra âm thanh. Lưu ý rằng nếu sinkId chứa một đối tượng AudioSinkInfo, điều đó cho biết âm thanh đã được đổi sang trạng thái không phát trên bất kỳ thiết bị đầu ra nào.
audioCtx.addEventListener("sinkchange", () => {
if (typeof audioCtx.sinkId === "object" && audioCtx.sinkId.type === "none") {
console.log("Audio changed to not play on any device");
} else {
console.log(`Audio output device changed to ${audioCtx.sinkId}`);
}
});
Xem ví dụ kiểm thử SetSinkId của chúng tôi để có mã hoạt động (đồng thời xem mã nguồn).
Thông số kỹ thuật
| Specification |
|---|
| Web Audio API> # eventdef-audiocontext-sinkchange> |