Chrome getUserMedia视频无法在localhost上运行

问题描述 投票:4回答:3

我正在尝试让Chrome的getUserMedia在我的localhost上运行。

我的index.php文件是:

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <script type="text/javascript" src="js/script.js"></script>
        <title></title>
    </head>
    <body>
<video id="MediaStreamVideo" autoplay=""></video>    
    </body>
</html>

我的script.js文件是:

var stream;
var video = document.getElementById('MediaStreamVideo'); 
navigator.webkitGetUserMedia(
    {video: true, audio: true}, // Options
    function(localMediaStream) { // Success
        stream = localMediaStream;
        video.src = window.webkitURL.createObjectURL(stream);
    },
    function(err) { // Failure
        alert('getUserMedia failed: Code ' + err.code);
    }
);

Chrome要求我允许使用相机和麦克风(我接受),但没有任何反应。

它在jsfidde上工作正常

有任何想法吗?

google-chrome video html5-video google-chrome-devtools getusermedia
3个回答
2
投票

问题应该是脚本执行的顺序。脚本执行时,视频元素“MediaStreamVideo”将不可用。

最后加载脚本始终是一个好习惯。另外,将代码包装在函数中并执行onload。


0
投票

如果您使用的是Chrome,并使用file:///网址,那么您需要使用--allow-file-access-from-files的命令行参数调用Chrome。

您可以查看this SO Q&A


0
投票

只需尝试:

var stream;
var video = document.getElementById('MediaStreamVideo'); 
navigator.webkitGetUserMedia(
    {video: true, audio: true}, // Options
    function(localMediaStream) { // Success
        stream = localMediaStream;
        //video.src = window.webkitURL.createObjectURL(stream);
        video.srcObject = stream;
   },
   function(err) { // Failure
      alert('getUserMedia failed: Code ' + err.code);
   }
);
© www.soinside.com 2019 - 2024. All rights reserved.