Chrome扩展程序 - getUserMedia抛出“NotAllowedError:由于关闭而失败” [英] Chrome Extension - getUserMedia throws "NotAllowedError: Failed due to shutdown"

查看:2549
本文介绍了Chrome扩展程序 - getUserMedia抛出“NotAllowedError:由于关闭而失败”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一套录制用户网络摄像头和麦克风的套件。它在网页上运行良好,但在Chrome扩展程序中行:

I have a suite which records the user's webcam and mic. It works great on a webpage, but in a Chrome Extension the line:

navigator.mediaDevices.getUserMedia({video: true, audio: true})
    .then(this.record.bind(this))
    .catch(VidRA.error);

投掷

NotAllowedError: Failed due to shutdown

我搜索过并发现几乎没有任何可能解释一下。有没有其他人遇到这个或有没有人知道我能做些什么呢?

I've searched and found almost nothing that might explain this. Has anyone else come across this or does anyone know what I can do about it?

推荐答案

哇,这是一个雷区。

首先,它似乎是一个错误(谢谢,@ wOxxOm)。

Firstly, it seems this is a bug (thanks, @wOxxOm).

所以我们需要围绕它编码。

So we need to code around it.

由于后台脚本生成了这个bug在请求媒体访问时,我们需要在其他地方请求它。如果您尝试使用弹出式JS,则会生成相同的错误,因此会留下内容脚本

Since the background script generates this bug when requesting media access, we need to request it elsewhere. The same error is generated if you try from the popup JS, so that leaves content scripts.

这些步骤是:


  • 内容脚本请求访问媒体设备

  • Content script requests access to media devices

成功时,内容脚本消息后台脚本

On success, content script messages background script

收到后消息,后台脚本请求访问媒体设备;由于内容脚本已经成功,后台脚本现在也会成功

On receipt of message, background script requests acccess to media devices; since content script has already succeeded, background script will now also succeed

重要的是,内容脚本必须在上下文中运行扩展名,而不是当前网页(在活动标签中。)

Crucially, the content script must run in the context of the extension, NOT the current webpage (in the active tab.)

我的方法是拥有一个持久内容脚本(在 content_scripts )当打开扩展程序时,其工作是将iframe注入当前选项卡的页面。

My approach was to have a persistent content script (specified in the manifest under content_scripts) whose job is, when the extension is opened, to inject an iframe into the current tab's page.

加载iframe来自扩展程序的页面,我们称之为 iframe.html

Into that iframe is loaded a page from the extension, let's call it iframe.html.

在该页面中,有一个JavaScript请求媒体访问。

In that page, there's JavaScript which requests media access.

Phew ...

这篇关于Chrome扩展程序 - getUserMedia抛出“NotAllowedError:由于关闭而失败”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆