如何使用 webrtc insde google chrome 扩展? [英] How to use webrtc insde google chrome extension?

查看:22
本文介绍了如何使用 webrtc insde google chrome 扩展?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发了一个谷歌浏览器扩展.

I have developped a google chrome extension.

我现在正在尝试将 webRTC 功能集成到其中:

I am trying now to integrate webRTC feature inside:

navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia;
console.log("step1");
navigator.getUserMedia({audio: true, video: true}, function(stream){
    console.log("step2");
    $('#myVideo').prop('src', URL.createObjectURL(stream));
    window.localStream = stream;
    console.log("step3");

  }, function(error){ 
    console.log(error);
 });

我有一个错误:

step1
NavigatorUserMediaError {constraintName: "", message: "", name: "InvalidStateError"}

有什么想法吗?

在我的扩展程序中使用 webrtc 需要任何特殊许可吗?是否可以在扩展中访问 webrtc?

Do I need any special permission to use webrtc inside my extension ? and is it possible to access webrtc in a extension ?

问候

这是我所谓的弹出窗口"的屏幕截图(扩展名 = 弹出窗口 + 背景)

Here is the screenshot of what I call the "popup" (extension = popup + background)

推荐答案

为了在 Chrome 扩展程序的后台页面中使用 WebRTC 或语音识别 API,您需要从扩展程序的选项卡中打开一个页面,(弹出)窗口或 iframe(在选项卡内)(如果您使用 iframe,请不要忘记在 web_accessible_resources).在该页面调用navigator.webkitGetUserMedia触发权限提示.用户批准权限后,您的扩展程序(特别是后台页面)可以再次请求访问麦克风/摄像头,该请求将自动获得批准.

In order to use WebRTC or the speech recognition API in the background page of a Chrome extension, you need to open a page from your extension in a tab, (popup) window or iframe (within a tab) (if you use an iframe, don't forget to list the page in web_accessible_resources). In this page, invoke navigator.webkitGetUserMedia to trigger the permission prompt. After the user approves the permission, your extension (in particular the background page) can request access to the microphone / camera again, and the request will automatically be approved.

是否需要在一次性提示后无声批准正在辩论,以及这也是 audioCapturevideoCapture 权限还不能在扩展中使用.audioCapturevideoCapture 权限在未来可用,所以如果您的扩展程序需要,我建议将这些权限放在您的清单文件中,即使当前版本的 Chrome 无法识别扩展程序的权限.

Whether silent approval after a one-time prompt is desirable is debated, and this is also the reason that audioCapture and videoCapture permissions cannot be used in extensions yet. The audioCapture and videoCapture permissions will become available in the future though, so I recommend putting these permissions in your manifest file if your extension needs it, even when the current versions of Chrome do not recognize the permission for extensions.

这篇关于如何使用 webrtc insde google chrome 扩展?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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