为何我的Chrome浏览器中VideoEncoder不可用?
解决Chrome中"Uncaught ReferenceError: VideoEncoder is not defined"报错
VideoEncoder属于WebCodecs API,Chrome最新版本虽原生支持,但出现该报错通常和以下原因有关,对应解决方法如下:
确保页面处于安全上下文环境
WebCodecs API受Chrome安全策略限制,仅允许在HTTPS协议或localhost(本地开发环境)下访问。如果页面通过普通HTTP(非localhost)打开,API会被禁用,直接调用就会触发引用错误。切换到HTTPS或本地localhost服务即可。检查WebCodecs是否被手动禁用
少数情况可能有人手动关闭了该功能。在Chrome地址栏输入chrome://flags/#enable-web-codecs,确认该选项设置为“Enabled”,之后重启浏览器生效。代码中增加兼容性判断
为避免环境不支持导致的报错,建议调用前先判断API是否存在:if ('VideoEncoder' in window) { // 正确实例化VideoEncoder并配置 const videoEncoder = new VideoEncoder({ output: (chunk) => { /* 处理编码后的视频块 */ }, error: (err) => { console.error('编码错误:', err); } }); videoEncoder.configure({ codec: 'avc1.42E01E', width: 1280, height: 720, bitrate: 5_000_000, framerate: 30 }); } else { console.error('当前环境不支持VideoEncoder API'); }
内容的提问来源于stack exchange,提问作者Keith Carter
相关产品推荐
相关产品推荐

