How do I stop all currently playing MediaElement players?
I'm wondering how to stop all the MediaElement players currently in the DOM. I've tried this:
$('video,audio').each(function() {
$(this)[0].player.pause();
});
Let me know if that works.
A quick and dirty one, but neither work.
$(".mejs-play").live('click',function(){
$(".mejs-pause").trigger('click');
});
Tried to do my homework on this one but can't seem to f开发者_StackOverflow社区ind a response anymore.
Try this...
$('video, audio').each(function() {
$(this)[0].pause();
});
Here's a simple way to do a "stop all".
When first creating your MediaElement players, create them each explicity (as opposed to using $('video,audio').mediaelementplayer()
):
var mediaElementPlayers = [];
$('video,audio').each(function(){
mediaElementPlayers.push(new MediaElementPlayer(this));
});
And then, when you want to stop them:
for (var i=0; i<mediaElementPlayers.length; i++){
mediaElementPlayers[i].pause(); // pause
if (mediaElementPlayers[i].getCurrentTime()){
mediaElementPlayers[i].setCurrentTime(0); // rewind
}
}
Very nice @Bart
And here is an addition to Barts function to stop audio in a certain context (within a certain element). Useful to stop media playing in a popup when it is closed for example:
function stopAudio(context) {
for (var i=0; i<mediaElementPlayers.length; i++){
if($(mediaElementPlayers[i].container).parents().find(context).length === 0) {
mediaElementPlayers[i].pause(); // pause
if (mediaElementPlayers[i].getCurrentTime()){
mediaElementPlayers[i].setCurrentTime(0); // rewind
}
}
}
}
Trigger the click event of pause button will work....check it here
$('.mejs-pause button').trigger("click");
精彩评论