MediaElement.js - How do you set duration before playing audio?

左心房为你撑大大i 提交于 2019-12-10 15:05:13

问题


Is there a way to preload the duration before pressing play the first time on an audio player. I know you can use { duration: 120 } which is fine if every clip is 120 seconds long. I do not have the information available in the database to dynamically populate this variable so there must be another way to tweak the MediaElement.JS script to do it.

Any thoughts anyone?


回答1:


I don't think mediaelement.js has a way of getting the duration before all or part of the file has been downloaded. You could play the video, poll the duration property until you get a value then reset the video to the start. Though that would not be very elegant.

You could used a JavaScript library like this client side: https://github.com/aadsm/JavaScript-ID3-Reader

If you are using PHP then you could do this server side. See PHP Function to get MP3 duration for more info.

Hope this helps.




回答2:


Let the browser load the metadata by setting preload attribute.

Syntax:

<audio preload="auto|metadata|none">

http://www.w3schools.com/tags/att_audio_preload.asp



来源:https://stackoverflow.com/questions/12356452/mediaelement-js-how-do-you-set-duration-before-playing-audio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!