WebRTC - Browser doesn't ask for mic access permission for local html file

12,677

Solution 1

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

Solution 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

Solution 3

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

Solution 4

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.

Solution 5

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://.

Share:
12,677
Amit
Author by

Amit

Updated on June 26, 2022

Comments

  • Amit
    Amit almost 2 years

    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?