1

I'm streaming audio to my player from Soundcloud API.

<audio></aidio>
<source src="soundcloud-track-url"></source>

I have onerror eventListener for <audio> and for <source> tags.

If stream is not loading (from source tag) it triggers onerror, but I don't know how to verify that this is because that the per day limit already reached.

So my logic: if I get an error on src load I want to split further, if this is because of "limits" - do this, if it is not because of "limits" - do that.

If per day limits are reached Soundcloud API return an HTTP 429 response code "Too Many Requests".

How do I can utilize "HTTP 429" information to get the expected result?

2 Answers2

1

You need to pass an attribute like event to your error handler and then get its target property which points to the HTML element that raised the error.

Once you have the element itself, it is easy to fetch its src attribute using .getAttribute('src').

Once you have the src attribute, you can fetch to it and check the error status for that request.

document.getElementById("mySource").onerror = event => {
    var sourceElement = event.target;
    var audioURL = sourceElement.getAttribute("src");

    fetch(audioURL).then(function(response) {
        alert(response.status);
    });
};

Here's sandbox on how this would work:

https://codepen.io/pghiran/pen/GzWdmg?editors=1010

Paul Ghiran
  • 1,233
  • 1
  • 16
  • 29
0

Since you have not provided any sort of clue in your question, I'm just guessing you're using AJAX to communicate with SoundCloud.

Given that, you need to check for status property on your xhr( XMLHttpRequest ) object.

xhr.onreadystatechange = function( e ) {

    // `xhr.status` is what you're looking for.
    // you can check if it matches your expected 429 or not 
    if ( xhr.status == 429 && xhr.readyState == 4 ) {

        // do whatever you want

    }

}

Update: due to using the wrong method at first place, I would suggest you read this same problem people had and look how they've solved it.

mrReiha
  • 908
  • 6
  • 24
  • Actually, no, one and only part of communication with Soundcloud is the url inside source tag (scr), no JS or other not involved here on my side @mrReiha –  Feb 01 '19 at 10:46
  • 1
    @AndrewK There is no way to get the HTTP status from a request made by an `audio` tag in JavaScript. The only way to do this is using an AJAX ( or fetch ) call from JS to be able to get the status code of request. – mrReiha Feb 01 '19 at 10:55
  • Can you possibly post it as an answer? So in case of error `onerror eventlistener function` get src url from the tag and check it via JS to HTTP 429 if it matches @mrReiha –  Feb 01 '19 at 10:59
  • @AndrewK I'm telling u, you have to set up an Ajax call and use that to get audio from sound cloud. I've updated my answer with link to a similar problem. – mrReiha Feb 01 '19 at 11:03