Andra Zeberga
Andra Zeberga

Reputation: 227

Catching invalid source error using videojs

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

player.src({
    src: 'http://example.com/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: enter image description here

Upvotes: 4

Views: 9659

Answers (2)

René Baudisch
René Baudisch

Reputation: 171

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
})

Upvotes: 0

Józef Podlecki
Józef Podlecki

Reputation: 11283

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.com/invalid',
    type: 'audio/mp4',
});
<script src="https://vjs.zencdn.net/7.7.6/video.js"></script>
<video id="player" ></video>

Upvotes: 3

Related Questions