Skip to content

Instantly share code, notes, and snippets.

@GiriAakula
Last active June 22, 2023 05:27
Show Gist options
  • Save GiriAakula/802eff120b942ae190f03a16a5f30cf6 to your computer and use it in GitHub Desktop.
Save GiriAakula/802eff120b942ae190f03a16a5f30cf6 to your computer and use it in GitHub Desktop.
This gist will explain how to get error events from videojs player.

Videojs Player Errors

To handle errors in Videojs player, you can use the following code:

const errorCodes = ["MEDIA_ERR_CUSTOM", "MEDIA_ERR_ABORTED", "MEDIA_ERR_NETWORK", "MEDIA_ERR_DECODE", "MEDIA_ERR_SRC_NOT_SUPPORTED", "MEDIA_ERR_ENCRYPTED"];

playerInstance.on("error", function() {
  sendEvent("playerError", errorCodes[playerInstance.error().code]);
});

This code snippet sets up an event listener for the "error" event in Videojs player. When an error occurs, it calls the sendEvent() function with the event name "playerError" and the corresponding error code based on playerInstance.error().code.

Videojs Ad Errors

To handle ad errors in Videojs player, you can use the following code:

playerInstance.on("adserror", function(e) {
  sendEvent("adError", `aderror-${e.data.AdErrorEvent.error.data.errorCode}`);
});

This code snippet sets up an event listener for the "adserror" event in Videojs player. When an ad error occurs, it calls the sendEvent() function with the event name "adError" and the specific ad error code extracted from e.data.AdErrorEvent.error.data.errorCode.

Note: The sendEvent() function is a generic function used to send events and register them in our analytics code. You should replace it with your own implementation or use an appropriate analytics library.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment