javascript - 使用 videojs 捕获无效源错误

标签 javascript video.js

我正在像这样将无效源加载到 videojs 播放器实例中:

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

然后我尝试按如下方式捕获发生的错误:

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

这给了我 videojs 内部抛出的错误,但不是导致它的错误。在我的应用程序中,如果您尝试加载不应该加载的源,我使用的 API 会抛出特定的错误代码。我怎么能捕捉到这样的错误。

我想捕获下面屏幕截图中的第一个错误: enter image description here

最佳答案

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

你不能。当 video 元素中的 src 属性元素发生变化时调用。

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>

关于javascript - 使用 videojs 捕获无效源错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61878817/

相关文章:

javascript - 解析并共享模块中的obj资源

javascript - 如何传递对象数组以在 Vue 2.0 中选择选项

iframe - 使用video.js时,为什么全屏按钮不能在iframe中使用?

javascript - video.js 插件不工作

javascript - 视频 : play loop video without re-load

javascript - 我有一个 JavaScript "Unexpected Identifier"错误。谁能帮我?

适用于客户端和 Node.js 服务器的 JavaScript 验证?

javascript - 人力车图表的 onClick 选项

javascript - 如何使用 VTT 文件在 VideoJS 中获取预览缩略图?

javascript - video.js .src() 函数无法识别