WebRTC getUserMedia不显示视频

时间:2012-11-14 17:19:42

标签: javascript webrtc

我有以下代码:

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>

为什么不显示视频?

1 个答案:

答案 0 :(得分:2)

您必须将url分配给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)