files/en-us/web/api/rtcdtmfsender/insertdtmf/index.md
{{APIRef("WebRTC")}}
The insertDTMF() method of the {{domxref("RTCDTMFSender")}} interface sends {{Glossary("DTMF")}} tones to the remote peer over the {{domxref("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.
insertDTMF(tones)
insertDTMF(tones, duration)
insertDTMF(tones, duration, interToneGap)
tones
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 {{optional_inline}}
interToneGap {{optional_inline}}
None ({{jsxref("undefined")}}).
InvalidStateError {{domxref("DOMException")}}
InvalidCharacterError {{domxref("DOMException")}}
tones is not valid DTMF (0-9, A-Z, #, or ,).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}}
{{Compat}}