6

I have some simple webRTC code which uses getUserMedia to gain access to user's mic. Now when I load that html file (saved at my localhost) in my browser, the browser doesn't ask for mic access permission and thus get failed to get access.

But when I run the same html inside the w3schools.com editor, it asks for mic access permission and upon allowing it to access my mic, it works fine...

Why is this strange behaviour?

Amit
  • 33,847
  • 91
  • 226
  • 299

7 Answers7

5

When you open an html file right off the filesystem (file:// prefix), Chrome will auto-block getUserMedia permissions. You have to run a server locally.

I started up a sinatra server like this:

# server.rb
require 'sinatra'

get '/' do
  File.read('index.html')
end

Then give it ago.

$ gem install sinatra
$ ruby server.rb

http://localhost:4567

Hugo
  • 172
  • 3
  • 9
2

Because of security Chrome won't open user media, e.g. WebCam when executing a file:/* document.

You could override however the security policy by starting chrome with the --disable-web-security command line option.

For testing check also the --use-fake-device-for-media-stream option.

N.B. When specifying command line options make sure there is no chrome/chromium process running. P.S Give it a try by creating a file test.html containing

<!DOCTYPE HTML>
<video autoplay/>
<script>
  navigator.webkitGetUserMedia({audio:true,video:true},
    function(stream){
      document.querySelector('video').src =
        URL.createObjectURL(stream);
    });
</script>

and than kill all chrome instances and start chrome like this:

 chrome.exe --use-fake-device-for-media-stream --disable-web-security test.html
siddhadev
  • 16,501
  • 2
  • 28
  • 35
2

This behavior is caused by Chrome security settings.

If you have PHP installed and you don't wanna setup Apache or other more advanced web server, probably the easiest way would be to run internal PHP web server this way (assuming you have your web files in /home/user/web/):

php -S 127.0.0.1:3000 -t /home/user/web/

Here is a description of the parameters:

-S <addr>:<port> Run with built-in web server.

-t <docroot> Specify document root <docroot> for built-in web server.

After you run the server start your browser and open this URL (assuming your test file is called webrtc.html):

http://127.0.0.1:3000/webrtc.html

Jan Krupa
  • 71
  • 3
1

Just some troubleshooting suggestions:

  • Check chrome://settings/content (scroll down to "Media"), to see if you've accidentally selected that site to always allow or always deny. (I'm on Chrome 26[dev]; this may be located somewhere else on Chrome 24.)

  • Also try restarting your browser - this bit of Chrome is still pretty buggy in my experience, and sometimes a restart fixes it.

  • And make sure you've got an error handler in your getUserMedia() call - there may be some additional info there.

Ken Smith
  • 20,305
  • 15
  • 100
  • 147
1

Are you loading the file via something like file://? It seems chromium does not give access to those files at all and completely ignores the request. Just tried myself and after uploading the file to a dev server it worked fine.

Even setting it to allow always it still does not work with file://.

boombatower
  • 651
  • 4
  • 6
1

You can't run HTML5 that uses getUsermedia API locally without using a local server. Use WampServer and place your HTML5 file inside the www folder.

Nisham Mahsin
  • 1,399
  • 5
  • 19
  • 43
0

This answer is for chrome

In Chrome you can use the --allow-file-access-from-files flag to allow webcam access from a local file.

Mac

On a mac you can open the terminal and type:

/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --allow-file-access-from-files /path/to/file.html

change /path/to/file.html with your path

Windows

In Windows you can create a shortcut. Right click Google Chrome And in the menu select: Copy To -> Desktop (shortcut), then right click the shortcut and click properties add the flag:

"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --allow-file-access-from-files C:\path\to\file.html

Hope this answer helps!

Andy
  • 2,892
  • 2
  • 26
  • 33