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

RTCRtpSender: dtmf property

· 52CE 我爱测

The read-only **`dtmf`** property on the RTCRtpSender interface returns an RTCDTMFSender you can use to send DTMF tones on this sender's audio track.

WebRTC

The read-only dtmf property on the RTCRtpSender interface returns an RTCDTMFSender you can use to send DTMF tones on this sender's audio track.

Value

An RTCDTMFSender if this is an audio sender, or null if it isn't.

Each audio sender gets its own RTCDTMFSender when it's created, so a connection sending two audio tracks has two of them. Senders for video tracks return null.

A non-null dtmf doesn't mean you can send tones yet. Tones travel in the RTP stream alongside the audio, so the sender must be connected and sending, and the two peers must have negotiated the audio/telephone-event codec. Check RTCDTMFSender.canInsertDTMF for that, or handle the InvalidStateError that RTCDTMFSender.insertDTMF throws.

Examples

Sending tones on an audio track

This example adds a microphone track to a connection, then sends a dial string once the connection is up. addTrack() returns the RTCRtpSender for the track, so there's no need to search for it.

const pc = new RTCPeerConnection(configuration);

async function dial(tones) {
  const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
  const [track] = stream.getAudioTracks();
  const sender = pc.addTrack(track, stream);

  // The track is an audio track, so the sender has a DTMF sender
  const dtmfSender = sender.dtmf;

  dtmfSender.addEventListener("tonechange", (event) => {
    if (event.tone === "") {
      console.log("Finished sending tones.");
    } else {
      console.log(`Sent tone: ${event.tone}`);
    }
  });

  pc.addEventListener("connectionstatechange", () => {
    if (pc.connectionState === "connected" && dtmfSender.canInsertDTMF) {
      dtmfSender.insertDTMF(tones);
    }
  });
}

Finding the audio senders on a connection

Because only audio senders have a dtmf object, you can use the property to pick them out of RTCPeerConnection.getSenders():

const audioSenders = pc.getSenders().filter((sender) => sender.dtmf);

Specifications

Browser compatibility

See also

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

资料来源与适用范围

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

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

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