My problem lies in a series of audiobuffersource calls and starting. I have successfully implemented pauses in the middle of an audioBuffer by stopping the audioNode and calculating the elapsedTime to start the audioBuffer again. That works just fine. The bug is where it moves from playing the current one to the next one, and it takes time (although very minimal) to start a completely new audioBuffer. When I call my pause() consecutively in a very short span of time it has this exception: speakCharacter.ts?8d1c:345 Uncaught (in promise) DOMException: Failed to execute 'stop' on 'AudioScheduledSourceNode': cannot call stop without calling start first.
I am pretty sure that this is caused by a somewhat asynchronized state of start because my current code is like this
this.audio.decodeAudioData(buffer)
.then((audioBuffer) => {
bufferSource.buffer = audioBuffer;
bufferSource.start(0, startTime);
this.playbackStarted = true;
console.log("this.playbackStarted=",this.playbackStarted);
if (onEnded) {
bufferSource.addEventListener("ended", () => {
onEnded();
this.playbackStarted = false;
console.log("this.playbackStarted=",this.playbackStarted);
});
}
});
and the console logs already tell me playbackStarted is true, but when it went to stop the audioNode, it still gives the exception.
Is there a way to check if the audio actually started? Or do I have to really manually check for there to be a short time elapsed before actually pausing / stopping the audioNode again?