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

RTCDTMFSender: insertDTMF() method

· 52CE 我爱测

The **`insertDTMF()`** method of the RTCDTMFSender interface sends DTMF tones to the remote peer over the RTCPeerConnection.

WebRTC

The insertDTMF() method of the RTCDTMFSender interface sends DTMF tones to the remote peer over the RTCPeerConnection.

Tones are sent asynchronously. Every time a tone starts or ends, a tonechange event is sent to the RTCDTMFSender.

You can call insertDTMF() at any time while the connection is active. Each call to insertDTMF() replaces any pending tones in the toneBuffer. You can abort sending queued tones by specifying an empty string ("") as the set of tones to play. Since insertDTMF() replaces the tone buffer, in order to add to the DTMF tones being played, it is necessary to call insertDTMF() with a string containing both the remaining tones (stored in the toneBuffer) and the new tones appended together.

Syntax

insertDTMF(tones)
insertDTMF(tones, duration)
insertDTMF(tones, duration, interToneGap)

Parameters

  • tones
    • : A string containing the DTMF codes to be transmitted to the recipient. Specifying an empty string as the tones parameter clears the tone buffer, aborting any currently queued tones. A comma character , in the string inserts a two-second delay. For example "12,34" will send tones for 1 and 2, pause for two seconds, and then send 3 and 4. Multiple commas add a longer delay, so ,, will add four seconds.
  • duration
    • : The amount of time, in milliseconds, that each DTMF tone should last. This value must be between 40 ms and 6000 ms (6 seconds), inclusive. The default is 100 ms.
  • interToneGap
    • : The length of time, in milliseconds, to wait between tones. The browser will enforce a minimum value of 30 ms (that is, if you specify a lower value, 30 ms will be used instead). The default is 70 ms.

Return value

None (undefined).

Exceptions

  • InvalidStateError DOMException
    • : Thrown if the DTMF tones cannot be sent because the track has been stopped or is in a read-only or inactive state.
  • InvalidCharacterError DOMException
    • : Thrown if one or more of the characters in tones is not valid DTMF (0-9, A-Z, #, or ,).

Examples

Using insertDTMF to send DTMF tones

This example shows how to use the insertDTMF() method to send tones over a WebRTC connection.

The code first checks if the canInsertDTMF property is defined, and if so, uses it to check whether inserting DTMF tones is supported. If the feature is supported, insertDTMF() is called to insert a tone.

if (sender.dtmf.canInsertDTMF) {
  const duration = 500;
  sender.dtmf.insertDTMF("1234", duration);
} else {
  console.log("DTMF function not available");
}

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

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

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