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?
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With