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

javascript - Catching invalid source error using videojs - Stack Overflow

programmeradmin2浏览0评论

I am loading an invalid source into videojs player instance like this:

player.src({
    src: '',
    type: 'audio/mp4',
});

Then I try to catch the occurring error as follows:

player.on('error', () => {
    console.log(player.error); //Gives MEDIA_ERR_SRC_NOT_SUPPORTED error
})

which gives me the error thrown internally by videojs, but not the error that caused it. In my application, I am consuming an API that throws specific error code if you are trying to load a source that you are not supposed to. How could I catch such an error.

I would like to catch the first error in the screenshot below:

I am loading an invalid source into videojs player instance like this:

player.src({
    src: 'http://example./invalid',
    type: 'audio/mp4',
});

Then I try to catch the occurring error as follows:

player.on('error', () => {
    console.log(player.error); //Gives MEDIA_ERR_SRC_NOT_SUPPORTED error
})

which gives me the error thrown internally by videojs, but not the error that caused it. In my application, I am consuming an API that throws specific error code if you are trying to load a source that you are not supposed to. How could I catch such an error.

I would like to catch the first error in the screenshot below:

Share Improve this question asked May 18, 2020 at 20:53 Andra ZebergaAndra Zeberga 2277 silver badges16 bronze badges 1
  • Well you are trying to use functionality that is not available through videojs, as far as I checked the documentation right now. What you could do is to check if the src returns a success response (like 2xx) and only then continue to initialise the player. You can use this as a starting point: stackoverflow./questions/5344145/… – brance Commented May 18, 2020 at 21:10
Add a ment  | 

2 Answers 2

Reset to default 3

I would like to catch the first error in the screenshot below:

You can't. It is invoked when src attribute element in video element changes.

However you can listen to it.

const playerElement = document.querySelector("#player")

playerElement.addEventListener("error", (event) => {
  console.log(event.target.error.message);
})

const player = videojs('player');

player.src({
    src: 'http://example./invalid',
    type: 'audio/mp4',
});
<script src="https://vjs.zencdn/7.7.6/video.js"></script>
<video id="player" ></video>

It is not documented and it is definitly implemented the wrong way, but I found a way to catch this errors!

Instead of using the 'mediaerror' that is never triggered but should be in this case you also cannot use the regular 'error' event, but the error event is thrown, but as 'aderror' (don't mix up with 'adserror').

So try this:

player.on('aderror', () => {
    console.log(player.error()); //Gives MEDIA_ERR_SRC_NOT_SUPPORTED error
})
发布评论

评论列表(0)

  1. 暂无评论