简体   繁体   中英

Chrome getUserMedia Not Requesting Permission Locally

I'm attempting to play around with navigator.getUserMedia in Chrome; however, it is not requesting permission when served locally (file:///whatever/index.html), but does on JSFiddle ( http://jsfiddle.net/EBsvq/ ) and other sites.

Does anyone know the reason for this? Do I need to somehow reset my permissions?

Here is what I am using locally:

<html>
<head></head>
<body>
   <button id="btn">Start</button>

<script type="text/javascript">
   /*! jQuery v1.8.3 jquery.com | jquery.org/license */
   //JQuery goes here
</script>

<script type="text/javascript">
  navigator.getUserMedia = navigator.webkitGetUserMedia || navigator.getUserMedia;

  $(function(){
    $('#btn').click(function(){
      navigator.getUserMedia({audio: true}, 
      function(){
        alert('success');
      },
      function (err) {
        alert('fail: ' + JSON.stringify(err));
      }); }); });
      </script>
  </body>
</html>

Chrome blocks a lot of stuff on file:/// URIs without reporting a security error (eg. Geolocation ). Your best option is to run from a local webserver, if you have Python installed try SimpleHTTPServer .

您可以在打开 chrome 时从命令行使用--allow-file-access-from-files标志,以便能够从本地文件系统使用getUserMedia

I encountered similar problem, but related to microphone access. Chrome blocks device access if you serve your file via file://(note that on Microsoft Edge and Firefox it worked via file:://).

One solution i've found for Chrome:

  1. open chrome an type in the URL chrome://version/
  2. copy the value of "Command Line" field in your command line and append --allow-file-access-from-files and run it
  3. after Chrome opened and enter your file:// url.

For starting Chrome always like that, right click on Chrome icon, and then click on properties. In the Shortcut tab append to the Target value all the command line parameters from 2.

It's a little non-trivial how to do the command line arg in OS X. It will usually look like this

"/Applications/Google Chrome.app/Contents/MacOS/Google Chrome" --allow-file-access-from-files

If you are a freak like me, and put your apps in ~/Applications , then it will be

"/Users/yougohere/Applications/Google Chrome.app/Contents/MacOS/Google Chrome" --allow-file-access-from-files

If neither of those are working, then type chrome://version in your Chrome address bar, and it will tell you what "command line" invocation you should be using. Just add --allow-file-access-from-files to that.

Try Installing Simple Server ( light weight ) , one click install , can be downloaded here

Once it is installed , refer the app to the page you want to run.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM