Open michaelrkn opened 4 years ago
This isn't as simple as it seems. You'd think you could just write:
connection.on('ringing', (hasEarlyMedia) => {
console.log(hasEarlyMedia);
if (!hasEarlyMedia) {
var audio = new Audio('ring.mp3');
audio.play();
}
});
But what happens is that ringing
will fire as soon as the dial is made, and hasEarlyMedia
will always be false at this point. Then, if there is actually a ringing sound, ringing
will fire again, and hasEarlyMedia
will be true.
So ideally after the first ringing
fires, we'd set a timer for 5 seconds (or something), and then start playing a ringing sound, and then stop the ringing sound if ringing
fires again, or if the call is answered, or if the connection is closed.
If the call does not produce "early media", we should play a recording of a ringing sound. https://www.twilio.com/docs/voice/client/javascript/connection#onringing-handlerhasearlymedia