I managed to successfully create a structure for recording and downloading audio files. However, I'm facing an issue where the final downloaded file has an unknown duration. Is there any way to solve this problem?? Here is my Typescript code snippet:
let recorder: MediaRecorder,
dataArray: Blob[]
async function InitializeAudio(): Promise<void> {
let audioIN = { audio: true }
try {
const mediastremObj = await navigator.mediaDevices.getUserMedia(audioIN)
const mediaRecorder = new MediaRecorder(mediastremObj)
recorder = mediaRecorder
recorder.ondataavailable = (ev)=>{
dataArray.push(ev.data)
}
recorder.onstop = (ev) => {
const downloadTag = document.querySelector('#file-download') as HTMLAnchorElement
const audioFile = new Blob(dataArray, {'type': 'audio/mp3;'})
const url = window.URL.createObjectURL(audioFile)
downloadTag.href = url
downloadTag.download = `my-${Date.now()}-audio.mp3`
downloadTag.click()
window.URL.revokeObjectURL(url)
dataArray = []
}
} catch (e) {
throw new Error(e)
}
}