In the audio preview section, the player doesn’t display the recorded audio duration, which can confuse users. Additionally, the video duration keeps updating as soon as the audio starts playing.
Below is a sample JS snippet that can help resolve the duration calculation issue.
const audio = new Audio();
audio.src = URL.createObjectURL(recordedBlob);
audio.addEventListener("loadedmetadata", () => {
if (audio.duration === Infinity) {
// Jump to a large time to trigger correct duration
audio.currentTime = 1e101;
audio.ontimeupdate = () => {
audio.ontimeupdate = null;
console.log("Correct duration:", audio.duration);
audio.currentTime = 0; // reset
};
} else {
console.log("Duration:", audio.duration);
}
});
