角度2和使用html 5视频的摄像机流的实例化

问题描述 投票:2回答:1

我是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片段中所示?

angular typescript html5-video
1个回答
8
投票

在您的模板中,您可以包含视频指令,如下所示:

<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

© www.soinside.com 2019 - 2024. All rights reserved.