缓存为空时获取音频文件的持续时间属性的问题 [英] Issue with getting duration attribute of an audio file when cache is empty

查看:165
本文介绍了缓存为空时获取音频文件的持续时间属性的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

获取音频/mpeg(mp3)的元数据时遇到问题.

I have a problem with getting the metadata of an audio/mpeg (mp3).

例如,我使用JS来获取音频文件的持续时间,并且当缓存为空时,持续时间值将返回"Infinity"(NaN).

For example, I'm using JS to get the duration of the audio file and when the cache is empty the duration value returns "Infinity" (NaN).

我尝试使用事件/属性预加载和onloadedmetadata,并且总是在缓存为空时无法获得音频的持续时间和其他属性.

I have tried with the events/attributes preload and onloadedmetadata and always when the cache is empty I can't get the duration and other properties of an audio.

注意:仅当我清理缓存时(或当访问者首次访问该页面时),问题就出现了.

Note: The problem is only when I clean the cache (or when the visitors comes first time to the page).

    audioElement = new Audio('http://www.html5rocks.com/en/tutorials/audio/quick/test.mp3');
    console.log(audioElement);
    audioElement.addEventListener("loadedmetadata", function(_event) {
    var duration = audioElement.duration;
    console.log( duration );
    });

推荐答案

也许是因为您的媒体正在流式传输吗?

Perhaps it is because your media is streaming?

请参见 https://developer.mozilla.org/zh-CN /docs/Web/API/HTMLMediaElement

duration只读double
媒体长度,以秒为单位;如果没有可用的媒体数据,则为零.如果媒体数据可用,但长度未知,则此值为NaN. 如果流媒体并且没有预定义的长度,则该值为Inf .

duration Read only double
The length of the media in seconds, or zero if no media data is available. If the media data is available but the length is unknown, this value is NaN. If the media is streamed and has no predefined length, the value is Inf.

这篇关于缓存为空时获取音频文件的持续时间属性的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆