Access MediaElement.js Player Attributes

2019-08-16 13:09发布

问题:

I need to access the properties of a MediaElementPlayer object, how can I do this? The website mentions you can access properties like currentTime and paused from the MediaElement object, but doesn't mention MediaElementPlayer. An example of what I'd like to do:

var mejsplayer = new MediaElementPlayer($("#myplayer"), mejsOptions);

setInterval(debug, 1000);
function debug() {
    console.log("Duration is " + mejsplayer.duration);
    console.log("Current time is " + mejsplayer.currentTime);
    console.log("Volume is " + mejsplayer.volume);
    ....
};

The above code reports that all of these variables are undefined.

回答1:

EDIT

If you want to access the properties of the player you should get the DOM object. MediaElement is just some sort of extension of the element so it still uses the DOM object. So try this:

new MediaElementPlayer($("#myplayer"), /*mejsOptions*/);
var player = document.getElementById('myplayer'); 
console.log(player.duration());

It is recommended to do this using the events provided, for instance:

<video id="player1" width="320" height="240" poster="poster.jpg" controls="controls" preload="none">
    <!-- MP4 for Safari, IE9, iPhone, iPad, Android, and Windows Phone 7 -->
    <source type="video/mp4" src="http://mediaelementjs.com/media/echo-hereweare.mp4" />
</video>

player = new MediaElementPlayer('#player1',{
    success: function (mediaElement, domObject, player) {
        mediaElement.addEventListener('timeupdate', function(e) {
            console.log("Duration is " + mediaElement.duration);
            console.log("Current time is " + mediaElement.currentTime);
            console.log("Volume is " + mediaElement.volume);
        });
    }
});

you can try it here on jsfiddle



回答2:

After a long time of working with MEJS I finally found the answer to this question. The MediaElementPlayer object encapsulates the MediaElement object, which can be accessed like this:

var mejsplayer = new MediaElementPlayer($("#myplayer"), mejsOptions);

setInterval(debug, 1000);
function debug() {
    console.log("Duration is " + mejsplayer.media.duration);
    console.log("Current time is " + mejsplayer.media.currentTime);
    console.log("Volume is " + mejsplayer.media.volume);
    ....
};