大网数据广告活动
Web 标准与前端开发

MediaStreamTrack: mute event

· 52CE 我爱测

The **`mute`** event is sent to a MediaStreamTrack when the track's source is temporarily unable to provide media data.

Media Capture and Streams

The mute event is sent to a MediaStreamTrack when the track's source is temporarily unable to provide media data.

When the track is once again able to produce media output, an MediaStreamTrack/unmute_event event is sent.

During the time between the mute event and the unmute event, the value of the track's MediaStreamTrack.muted property is true.

[!NOTE] The condition that most people think of as "muted" (that is, a user-toggled state of silencing a track) is actually managed using the MediaStreamTrack.enabled property, for which there are no events.

This event is not cancelable and does not bubble.

Syntax

Use the event name in EventTarget/addEventListener:

addEventListener("mute", (event) => { })

Alternatively, assign to the event handler property of the interface:

onmute = (event) => { }

Event type

A generic Event.

Examples

In this example, event handlers are established for the mute and MediaStreamTrack.unmute_event events in order to detect when the media is not flowing from the source for the MediaStreamTrack referenced by musicTrack.

musicTrack.addEventListener("mute", (event) => {
  const widget = document.getElementById("timeline-widget");
  widget.style.backgroundColor = "#aaaaaa";
});

musicTrack.addEventListener("unmute", (event) => {
  document.getElementById("timeline-widget").style.backgroundColor = "white";
});

With these event handlers in place, when the track musicTrack enters its MediaStreamTrack.muted state, the element with the ID timeline-widget gets its background color changed to #aaaaaa. When the track exits the muted state—detected by the arrival of an unmute event—the background color is restored to white.

You can also use the onmute event handler property to set up a handler for this event; similarly, the MediaStreamTrack.unmute_event event handler is available for setting up a handler for the unmute event. The following example shows this (but note that this replaces other event listeners added using onmute and onunmute, if any):

musicTrack.onmute = (event) => {
  document.getElementById("timeline-widget").style.backgroundColor = "#aaaaaa";
};

musicTrack.onunmute = (event) => {
  document.getElementById("timeline-widget").style.backgroundColor = "white";
};

Mute tracks through receivers

The following example shows how to mute tracks using receivers.

// Peer 1 (Sender)
const transceivers = peer.getTransceivers();

const audioTrack = transceivers[0];
audioTrack.direction = "recvonly";

const videoTrack = transceivers[1];
videoTrack.direction = "recvonly";

// Peer 2 (Receiver)
audioTrack.addEventListener("mute", (event) => {
  // Do something in UI
});

videoTrack.addEventListener("mute", (event) => {
  // Do something in UI
});

transceivers is an array of RTCRtpTransceiver where you can find the audio or video track sent and received. For more information, see the RTCRtpTransceiver.direction article.

Specifications

Browser compatibility

See also

  • MediaStreamTrack/unmute_event event
  • RTCRtpTransceiver.direction

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。