简体   繁体   English

如何访问 angular2 中的视频元素属性?

[英]How can I access video element properties in angular2?

I cannot access the video element properties until I have played the video.在播放视频之前,我无法访问视频元素属性。

I am currently checking the video duration in ngOnInit and NaN value is being returned.我目前正在检查ngOnInit的视频持续时间,并且正在返回NaN值。

My lines of code to get duration are:我获取持续时间的代码行是:

HTML: HTML:

<video src="myvid.mp4" #vid></video>

TypeScript:打字稿:

    @ViewChild('vid') vid: ElementRef
    ngOnInit(){
      //vid is of type ElementRef
      this.duration = this.vid.nativeElement.duration
    }

The video's metadata is not being loaded so properties such as duration cannot be accessed until the video is played.未加载视频的元数据,因此在播放视频之前无法访问duration等属性。 To load the metadata, add the preload attribute of the video element with the value metadata .要加载元数据,请添加值为metadata的 video 元素的preload属性。 You should then add an event handler for the loadedmetadata event.然后,您应该为loadedmetadata事件添加一个事件处理程序。 Here are the changes I have made:以下是我所做的更改:

HTML HTML

<video src="myvid.mp4" preload="metadata" (loadedmetadata)="vidProps($event)"></video>

TypeScript打字稿

@ViewChild('vid') vid: ElementRef
vidProps(event){
    this.duration = event.srcElement.duration
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM