问题
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