I am trying to get .mp3 file from backend and displaying playable audio. So backend is converting .mp3 file to base64 and sending me. Tried to send .mp3 directly, but it doesn't work.
I am getting a mp3 converted to base64 from backend like this
axios
.post(`abcEndPoint`, {
text: textValue
})
.then((res) => {
setAudio(res.data)
console.log(res.data)
});
The res.data is as follows
Now I am converting this base64 response to audio tag readable Url with function.
function base64ToImageUrl(base64String) {
return `data:audio/mpeg;base64,${base64String}`;
}
I have tried to change format to audio/wav, .mp3 , audio/mp3
And lastly, here is my jsx for audio
<>
<audio controls>
<source src={base64ToImageUrl(audio)} />
Your browser does not support the audio element.
</audio>
</>
What is that I'm doing wrong, or is it even doable in react/JavaScript