How to use webrtc insde google chrome extension?

岁酱吖の 提交于 2019-12-18 07:05:42

问题


I have developped a google chrome extension.

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);
 });

I got an error:

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

Any idea ?

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

regards

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


回答1:


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.

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.



来源:https://stackoverflow.com/questions/29177097/how-to-use-webrtc-insde-google-chrome-extension

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!