jquery - 缓存为空时获取音频文件的持续时间属性的问题

标签 jquery html audio mp3 metadata

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

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

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

注意:问题仅在我清除缓存时(或当访问者第一次访问该页面时)。

    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 );
    });

最佳答案

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

参见https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement

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.

关于jquery - 缓存为空时获取音频文件的持续时间属性的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16058178/

相关文章:

javascript - 如何确保使用css和js的动态文本可以缩放和向上滑动?

javascript - 可以滚动内容但不出现滚动条

javascript - jQuery DataTable 列单元格中的中心元素

html - 导航栏中的中心品牌

html - 为什么我的常规导航栏会覆盖标题而折叠的导航栏不会? ( Bootstrap/整页)

iphone - iPhone应用程序的声音识别?

javascript - 由于 svg,图表圈未显示在 fiddle 中

jquery - JQGrid 中的列自动换行

swift - 访问 AVAudioPCMBuffer 的 Float 样本进行处理

matlab - MATLAB:停止播放声音