Angular 2 并使用 html 5 视频实例化相机流

Angular 2 and instantiation of a camera stream with html 5 video

我是 Angular 2.

的新手

如果我有视频标签,例如:

<video width="480" height="480" autoplay></video>    

还有一个 javascript 打开相机流的示例片段:

var video = document.getElementById('video');
if(navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
  navigator.mediaDevices.getUserMedia({ video: true }).then(function(stream) {
      video.src = window.URL.createObjectURL(stream);
      video.play();
  });
}

在 Angular 2 + Typescript 我想我可以像这样访问视频标签:

@Component({
  selector: 'video-component',
  template: `
    <video #videoplayer autoplay></video>
  `
})
export class Video {
 @ViewChild('videoplayer') videoPlayer;
  ngAfterViewInit() {
    let video = document.getElementById('video');

    // How to access the mediadevice ??

  }
}

如何访问媒体设备并实例化流,如 javascript 片段中所示?

在您的模板中,您可以像这样包含视频指令:

<video #video width="640" height="480" autoplay></video>

然后在你的组件中:

@ViewChild('video') video:any; 
// note that "#video" is the name of the template variable in the video element

ngAfterViewInit() {
  let _video=this.video.nativeElement;
  if(navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
    navigator.mediaDevices.getUserMedia({ video: true })
                          .then(stream => {
                            _video.src = window.URL.createObjectURL(stream);
                            _video.play();
                          })
  }
}

在 stackblitz 上查看:https://stackblitz.com/edit/live-video