最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - No error event when media sources fail to load - Stack Overflow

programmeradmin1浏览0评论

I have an audio element with several sources, all provided by a streaming service. We ran in to a user cap, so the streams were failing with 403 http errors.

After each source failed and a separate error appeared in console ("Max listeners reached"), a final error was output in console,

"All candidate resources failed to load. Media load paused."

However, no error event was fired on the element. I tried script attachment:

$(document).ready(function () {
    $('#audioPlayerElement').on('error', function () {
        // does not get triggered
    });

    $('#audioPlayerElement').error(function () {
        // does not get triggered
    });
});
$(document).on('error', '#audioPlayerElement', function () {
    // does not get triggered
});
document.getElementById('audioPlayerElement').onerror = function () {
    // does not get triggered
};

... I tried inline javascript:

<script>
    var playerError = function () {
        // does not get triggered
    }
</script>
<audio onerror="playerError()"...

... nothing.

This is my HTML (with the URLs redacted):

<audio id="audioPlayerElement" controls preload="auto" >
    <source src="http://[STREAM_URL].ogg" type="audio/ogg">
    <source src="http://[STREAM_URL].mp3" type="audio/mp3">
    <source src="http://[STREAM_URL].wav" type="audio/wav">
</audio>

Am I missing something? I read through the media events documentation on MDN and did not see any different event specific to this situation, error seems to be the appropriate thing to use. My goal is to detect the fact that the media isn't going to load and municate that to the user.

Tested in Firefox, Safari, and Chrome with same results.

I have an audio element with several sources, all provided by a streaming service. We ran in to a user cap, so the streams were failing with 403 http errors.

After each source failed and a separate error appeared in console ("Max listeners reached"), a final error was output in console,

"All candidate resources failed to load. Media load paused."

However, no error event was fired on the element. I tried script attachment:

$(document).ready(function () {
    $('#audioPlayerElement').on('error', function () {
        // does not get triggered
    });

    $('#audioPlayerElement').error(function () {
        // does not get triggered
    });
});
$(document).on('error', '#audioPlayerElement', function () {
    // does not get triggered
});
document.getElementById('audioPlayerElement').onerror = function () {
    // does not get triggered
};

... I tried inline javascript:

<script>
    var playerError = function () {
        // does not get triggered
    }
</script>
<audio onerror="playerError()"...

... nothing.

This is my HTML (with the URLs redacted):

<audio id="audioPlayerElement" controls preload="auto" >
    <source src="http://[STREAM_URL].ogg" type="audio/ogg">
    <source src="http://[STREAM_URL].mp3" type="audio/mp3">
    <source src="http://[STREAM_URL].wav" type="audio/wav">
</audio>

Am I missing something? I read through the media events documentation on MDN and did not see any different event specific to this situation, error seems to be the appropriate thing to use. My goal is to detect the fact that the media isn't going to load and municate that to the user.

Tested in Firefox, Safari, and Chrome with same results.

Share Improve this question edited Aug 28, 2014 at 18:09 Chris Baker asked Aug 26, 2014 at 15:56 Chris BakerChris Baker 50.6k12 gold badges99 silver badges116 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7 +50

I was curious so I played around with this and found that if you attach the onerror to the individual sources your event handler function will be triggered.

<audio id="audioPlayerElement" controls preload="auto">
  <source src="whatever" type="audio/ogg" onerror="playerError()">
  <source src="whatever" type="audio/mp3" onerror="playerError()">
  ...
</audio>

Is it possible to check the response headers for the media URLs separately? If so, you could request just headers for the media URLs, check for anything other than 200, and then provide feedback to the user. Might be a workaround. Your code looks fine to me.

发布评论

评论列表(0)

  1. 暂无评论