【发布时间】:2019-08-07 02:00:13
【问题描述】:
选择上传文件时,如何以角度形式获取视频/音频持续时间?
我已通过此事件侦听器获取 .html 格式的每个文件:
(change)="fileSelected($event)"
在 .ts 文件中有这个代码:
attachedFile;
fileSelected($event) {
this.attachedFile = <File>event.target.files[0]
var video = document.createElement('video');
video.preload = 'metadata';
video.onloadedmetadata = function() {
window.URL.revokeObjectURL(video.src);
var duration = video.duration; // here we have duration time but couldn't use out of this scope
}
video.src = URL.createObjectURL(this.attachedFile);
//not available in fileSelected function scope
console.log(duration); //undefined
}
var foo = duration; // undefined
【问题讨论】:
-
Typescript 的代码具有非阻塞性。所以,我认为在调用
video.onloadedmetadata函数时,console.log(duration)已经执行了。 -
但它在 fileSelected 函数中不可用,因此只能在声明之外使用 console.log
-
你得到的是
undefined错误而不是Reference Error这意味着同样的语句适用于这里,在设置持续时间之前执行其余代码。 -
它是正确的,但没有办法将它们分开并且它们同时执行
标签: angular metadata angular7 duration