【问题标题】:Angular 2 and instantiation of a camera stream with html 5 videoAngular 2和带有html 5视频的相机流的实例化
【发布时间】:2019-02-26 05:33:35
【问题描述】:

我是 Angular 2 的新手。

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

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

还有一个 javascript 示例 sn-p 打开相机流:

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 sn-p 中所示?

【问题讨论】:

  • 说真的,没人?我完全不喜欢我的方法了吗?

标签: angular typescript html5-video


【解决方案1】:

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

<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

【讨论】:

  • @DavidD。我对 plumker 也有一些问题,所以我上传了一个版本到 stackblitz.com。希望对您有所帮助。
  • 非常感谢。在 macOS 中,适用于 Firefox 和 Safari,但不适用于 Chrome,Could not start video source (NotReadableError:undefined)
  • 无法播放您的问题,甚至尝试在多个应用中打开相机。如果可以重现并找到解决方案,将会回来。
  • 问题已解决,这只是 macOS 上操作系统级别的问题。我必须先去安全部门并允许 Chrome 访问摄像头和麦克风。
  • 伟大的@DavidD。很高兴听到这个消息。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-03-14
  • 1970-01-01
  • 1970-01-01
  • 2018-09-11
  • 1970-01-01
  • 2016-03-06
  • 1970-01-01
相关资源
最近更新 更多