function stopRingtone() {
if (!isRingtonePlaying) return;
if (ringtoneTimeout) {
clearTimeout(ringtoneTimeout);
ringtoneTimeout = null;
}
if (sourceNode) {
sourceNode.stop();
sourceNode.disconnect();
sourceNode = null;
}
if (gainNode) {
gainNode.disconnect();
gainNode = null;
}
if (audioContext) {
audioContext.close().then(() => {
audioContext = null;
audioBuffer = null;
console.log("AudioContext closed and reset.");
}).catch((err) => {
console.error("Error closing AudioContext:", err);
});
}
}
function playAudioLoop() {
sourceNode = audioContext.createBufferSource();
sourceNode.buffer = audioBuffer;
sourceNode.connect(gainNode);
sourceNode.start(0);
const loopDuration = audioBuffer.duration * 1000; // Convert to milliseconds
const targetDuration = 12000; // 12 seconds in milliseconds
ringtoneTimeout = setTimeout(() => {
if (isRingtonePlaying) {
const elapsedTime = audioContext.currentTime * 1000;
if (elapsedTime < targetDuration) {
playAudioLoop(); // Continue looping
} else {
stopRingtone(); // Stop after target duration
}
}
}, Math.min(loopDuration, targetDuration));
}
Pause the audio when stopRingtone() function is called.on implementing Custom ringtone for call, on accepting or other call events audio should be stopped , here it is not stopping , it continuously keeps on playing