I am building a web application that streams audio from an API using the AudioContext API. However, there are audio glitches when playing on iOS that need to be fixed.
const playNextAudioChunk = () => {
if (audioBufferQueue.length > 0) {
isPlaying = true;
const audioBuffer = audioBufferQueue.shift();
const source = audioContext.createBufferSource();
source.buffer = audioBuffer;
source.connect(gainNode);
const currentTime = audioContext.currentTime;
if (startTime === 0) {
startTime = currentTime + 0.1; // slight delay to start the first chunk
}
// Schedule the chunk to play at the correct time
source.start(startTime + offset);
offset += audioBuffer.duration;
source.onended = () => {
isPlaying = false;
playNextAudioChunk();
};
} else {
setValidation(false);
isPlaying = false;
setAudioData(null);
setOpenMicModel(false);
}
};
New contributor
Pradeep Sharma is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.