0

So I am quite familiar with JavaScript -strictly Vanilla JS. I was recently making a radio streaming site which is working quite well.

My issue is the fact that I can not/do not know how to catch an error from the stream URL. I feel like my code is quite cringe as this is all I do:

player.src = stationsData[id].url;
play.play();

I want to be able to catch all/some of the http errors because in case the link has issues, the whole program 'crashes'. I am familiar with async-await but only with JSON and txt files. Kindly help. I am using the audio html tag to play the audio.

<audio src="" class="audio-player" ></audio>
Kochez
  • 673
  • 4
  • 10
  • Start by attaching a listener to [`player.onerror`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement/error_event) – Bergi Jul 29 '21 at 19:57
  • By the way, I tried wrapping it with a try-catch, and it wasn't working, but I will research on that onerror @Bergi – Kochez Jul 29 '21 at 20:08
  • [`play()`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement/play) returns a promise, and [synchronous `try`/`catch` does not handle asynchronous promise rejections](https://stackoverflow.com/q/24977516/1048572). Use it with `await`, or use `.catch(…)`. – Bergi Jul 29 '21 at 20:13

0 Answers0