Search code examples
javascripthttphttpresponsehtml5-audioaudio-streaming

How can i check if an audio is empty?


const radio = new Audio('http://myradiostation.com/stream');

Is there a way to know if radio is empty (has no sound/audio)?

Sometimes the radio host forgets to turn on the stream, and I would like to give the end user some feedback when this occurs. Something like an alert that says "The radio host forgot to turn on the stream!!!"

What I want to achieve is something like this:

if(radio.isEmpty){
    alert('The radio host forgot to turn on the stream!!!')
}else{
    radio.play()
}

Thanks in advance.


Solution

  • The following did the trick:

     const radio = new Audio('http://myradiostation.com/stream');
    
      radio
        .play()
        .then(() => {
          console.log('success');
        })
        .catch((error) => {
          console.log(error);
          alert('The radio host forgot to turn on the stream!!!');
        });