Detect when HTML5 audio stream breaks or pauses
<p>I am trying to stream a Shoutcast URL using HTML5 Audio on a Cordova app.</p>

<p>The problem I have run into is this: There appears to be no callback that fires when an audio stream <em>loses connection</em> to the ShoutCast URL. At this stage, the audio element shows that it is playing the audio, but there is no audio.</p>

<h2>Code</h2>

<pre><code>Radio = {
    initialized: false,
    isBuffering: false,
    interrupted: false,
    isPlaying: false,
    media: null,
    trackName: '',
    url: 'shoutcast_url',
    initialize: function () {
        if (!this.media) {
            this.media = new Audio(this.url);

            this.media.preload = "none";

            this.media.onerror = function (e) {
                App.alert("Unable to connect to Radio");
                Radio.interrupt();
            };

            this.media.onwaiting = function (e) {
                Radio.set_buffering(true);
            };

            this.media.onplaying = function (e) {
                Radio.set_buffering(false);
            };
        }
    },
    set_buffering: function (value) {
    ...
    }
</code></pre>

<h2>Scenario</h2>

<ul>
<li>Connect to the Internet (say, through a hotspot) and start the audio radio.</li>
<li>Disconnect the hotspot from the Internet.</li>
</ul>

<p>After the buffered content is played, the audio stops playing. But no callbacks are fired that indicate loss of connection.</p>

<ul>
<li><code>media.networkState</code> is <code>2</code> (NETWORK_LOADING)</li>
<li><code>media.playbackRate</code> is <code>1</code> (Playing forward at normal rate)</li>
<li><code>media.readyState</code> is <code>4</code> (HAVE_ENOUGH_DATA)</li>
<li><code>media.preload</code> is <code>none</code></li>
</ul>

<p>The callbacks that I tried, (which did not fire when connection was lost) are:</p>

<ul>
<li><code>onstalled</code></li>
<li><code>onreset</code></li>
<li><code>onsuspend</code></li>
<li><code>onerror</code></li>
<li><code>onprogress</code></li>
<li><code>onwaiting</code></li>
</ul>

<p><strong>Question</strong> - Is there an audio callback that will fire when it is unable to play the audio due to lack of connection?</p>

<p>If not, is there any method which will update <code>readyState</code> or <code>networkState</code>? If so, I could just set a timer to check these values.</p>