简体   繁体   English

MediaElement.js会停止所有玩家

[英]MediaElement.js stop all players

I've been playing around with MediaElement.js for a while now and have a new application that uses 20 players on a single page. 我已经玩了一段时间的MediaElement.js并且有一个新的应用程序,在一个页面上使用20个玩家。 I'd like to be able to have the songs stop when another one is played - and not to overlap the tracks when they are playing. 我希望能够在播放另一首歌时让歌曲停止 - 而不是在播放时重叠曲目。 I can't seem to find an easy way to hack this - perhaps someone knows of a way? 我似乎找不到一个简单的方法来破解这个 - 也许有人知道一种方式?

Thanks. 谢谢。

In jQuery, you could do 在jQuery中,你可以做到

$('video,audio').each(function() {
      $(this)[0].pause();
});

Let me know if that works. 如果有效,请告诉我。

MediaElement.js creates a global object 'mejs' which has attribute 'players' which contains information about all players you`ve been created. MediaElement.js创建一个全局对象'mejs',其属性'players'包含有关您创建的所有玩家的信息。 Players are named mep_0, mep_1 etc. 玩家名为mep_0,mep_1等。

If you are using underscore/lodash you can write something like 如果你使用下划线/ lodash你可以写类似的东西

_.each(mejs.players, function(player) {
    player.media.stop();
});

If you need pure js code you can use 'for' operator 如果您需要纯js代码,可以使用'for'运算符

for (var player in mejs.players) {
    mejs.players[player].media.stop();
}

The easiest I found with jQuery: 我用jQuery找到的最简单:

$('video,audio').trigger('pause');

One-liner for the win! 赢得一线! It also avoid JS errors because it doesn't target directly the elements, thanks jQuery. 它还避免了JS 错误,因为它不直接针对元素,感谢jQuery。

A quick and dirty one ;) 快速而肮脏的一个;)

$(".mejs-play").live('click',function(){
  $(".mejs-pause").trigger('click');                              
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM