Access MediaElement.js Player Attributes

柔情痞子 提交于 2019-12-13 05:13:39

问题


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


来源:https://stackoverflow.com/questions/26171989/access-mediaelement-js-player-attributes

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