I'm building app to listen sound/music from the list.
Each item has an id to download mp3 file from API.
How can I get the mp3 from API and then correctly play the audio? Do I need to store the file or there is a way to dynamically load and play this file?
I'm receiving error like play audio error: DOMException: Failed to load because no supported source was found.
Here is my code:
const playAudio = async (id) => {
try {
const response = await axiosClient.get(`api/getaudio/id/${id}`)
const mp3 = new Blob([response.data], { type: 'audio/mp3' })
const url = window.URL.createObjectURL(mp3)
const audio = new Audio(url)
audio.load()
await audio.play()
} catch (e) {
console.log('play audio error: ', e)
}
}
return (
<ul>
{props.topList.map(item => (
<li onClick={() => playAudio(item.id)}>{item.title}</li>
))}
</ul>
)