0

我有以下代码:

document.addEventListener('DOMContentLoaded', function () {

  var video = document.querySelector('video');

  window.navigator.webkitGetUserMedia({ video: true, audio: true }, function (stream) {

    var url = window.webkitURL.createObjectURL(stream);
    video.src = stream;

  }, function (err) {
    console.log('error: ', err);
  });

});

html:

<!DOCTYPE html>
<html>
<head>
  <meta charset='utf-8'>
  <title>getUserMedia Video Example</title>
  <script src='index.js'></script>
</head>
<body>
  <video controls autoplay />
</body>
</html>

为什么不显示视频?

4

1 回答 1

2

您必须分配urlto video.src,而不是stream

navigator.webkitGetUserMedia({ video: true, audio: true }, function ( stream ) {
    video.src = window.webkitURL.createObjectURL( stream );
}, function ( err ) {
    console.log( 'error: ', err );
});​

现场演示: http: //jsfiddle.net/FcTMk/2/(仅限 Webkit)

于 2012-11-14T17:46:41.547 回答