You need a second for
loop that will output the stop function
for all the players except the current one, like this:
<?php
$posts = get_posts(array(
'numberposts' => 3,
'post_type' => 'any'
));
$number_of_posts_returned = count($posts); //because it can be less than the ones we wanted
if ($posts):
$i = 1;
?>
<?php foreach ($posts as $post) : ?>
<div class="media-container audio player-<?php echo $i; ?>">
<div id="player-<?php echo $i; ?>">Loading this Audio...</div>
<script type="text/javaScript">
var playerInstance = jwplayer("player-<?php echo $i; ?>");
playerInstance.setup({
file: '<?php //the_field("audio_upload"); ?>',
image: '<?php //the_field("audio_image"); ?>',
events:{
onPlay: function() {
<?php
//lets create the stop call for all except the current number
for ($x = 1; $x <= $number_of_posts_returned; $x++) {
if($x != $i){
?>
jwplayer('player-<?php echo $x; ?>').stop();
<?php }//end IF
} //end for
?>
}
}
});
</script>
<div class="media-details">
<h2 class="audio-title"><?php //the_field('name_of_audio'); ?> </h2>
</div>
</div>
<?php $i++;
endforeach;
wp_reset_postdata();
endif; ?>