It's difficult to pinpoint exactly why you cannot do a 2nd playback, I think it lies in your URL you are using to create the audio object as it might be getting invalidated or not properly handled after the first playback. One possible reason for this is that the URL created using 'URL.createObjectURL(audioBlob)' might not be released properly after each playback, causing conflicts when you try to create a new URL for the same audio data.
To fix this (if it is the culprit), you should make sure to revoke the object URL using 'URL.revokeObjectURL()' when you're done using it -
HTML DOM revokeObjectURL() method[
^]. You can do this in your 'stopRecording' function after setting the audio state -
const stopRecording = () => {
setRecordingStatus("inactive");
if (!mrRef.current) return;
mrRef.current?.stop();
mrRef.current.onstop = async () => {
const audioBlob = new Blob(audioChunksRef.current, {
type: dataType
});
const audioUrl = URL.createObjectURL(audioBlob);
setAudio(audioUrl);
if (audio) {
URL.revokeObjectURL(audio);
}
};
};