这里有一个简单的解决方案,它只需要从当前加载的玩家那里获取ID。
首先,为玩家div添加一个公共类:
<div id="player-<?php echo $i; ?>" class="jwplayer-loaded">Loading this Audio...</div>
然后在脚本中,获取所有当前加载的玩家,并逐个停止他们,但当前玩家除外:
<script type="text/javaScript">
// don\'t forget to esc_js() any values you print to javascript code
// https://developer.wordpress.org/reference/functions/esc_js/
var playerId = "player-<?php echo esc_js( $i ); ?>",
playerInstance = jwplayer( playerId );
playerInstance.setup({
file: \'<?php the_field("audio_upload"); ?>\',
image: \'<?php the_field("audio_image"); ?>\',
events:{
onPlay: function() {
var playerList = document.querySelectorAll(\'.jwplayer-loaded\');
[].forEach.call( playerList, function( v ) {
if ( v.id !== playerId ) {
jwplayer( v.id ).stop();
}
} );
}
}
});
</script>