首页 > 解决方案 > 如何以角度形式获取所选文件的音频或视频持续时间

问题描述

以我的角度形式,我通过以下方式获取我的视频或音频文件

(change)="fileSelected($event)

.html 格式和

attachedFile = <File>event.target.files[0]

在具有名称、类型和大小的 .ts 文件中。如何获得音频/视频持续时间?

标签: angularangular7angular-formsangular-file-upload

解决方案


您可以根据所选文件类型动态创建音频或视频元素,然后获取其他信息。这是代码...

fileSelected($event) {
  attachedFile = <File>event.target.files[0]

  let duration:any;

  //here you can check the file type for attachedFile either video or audio

  var video = document.createElement('video');
  video.preload = 'metadata';

  video.onloadedmetadata = function() {
    window.URL.revokeObjectURL(video.src);
    duration = video.duration; // here you could get the duration
  }

  video.src = URL.createObjectURL(attachedFile);
}

推荐阅读