Name is required.
Email address is required.
Invalid email address
Answer is required.
Exceeding max length of 5KB

Stop rest players and play the current one when multiple players


How i can stop all players and play the current one? Here is my code

jQuery('.musicitem').each(function(){
var getCurrentMusicLink = jQuery(this).find('#getCurrentMusicLink').val();
var getCurrentPost = jQuery(this).find('#currentpostID').val();
var musicaudiotrack = jQuery(this).find('.musicaudiotrack');
var postid = "musicaudiowrap"+getCurrentPost;
var playerInstance = jwplayer(postid).setup({
width: "100%",
androidhls: 'true',
type: "hls",
fallback:true,
sources: [
{
file: getCurrentMusicLink,
}
],
pauseMedia : function(playingMediaId) {
jQuery('.musicaudioinnerwrap').find('.jwplayer, object').each(function(){
var currentMediaId = jQuery(this).attr('id');
if(jwplayer(this).getState() == "PLAYING" || jwplayer(this).getState() == "BUFFERING" ) {
if(currentMediaId != playingMediaId){
jwplayer(this).play(false);
}
}
});
},
autostart: false,
logo: {
hide: true
}
});
jQuery(this).find('.musicplaybutton').click(function(){
if(jQuery(this).parent().parent().parent().siblings().find('.musicaudioinnerwrap').is(':visible')){
jQuery(this).parent().parent().parent().siblings().find('.musicaudioinnerwrap').hide();
jQuery(this).parent().parent().parent().siblings().find('.musicaudiotracktopwrap').show();
}
jQuery(this).parent().hide();
jQuery(this).parent().parent().find('.musicaudioinnerwrap').show();
var currentmusictitle = jQuery(this).parent().parent().parent().find('.musictitlewrap .musicmaintitle').text();
var currentmusicpublisheddate = jQuery(this).parent().parent().parent().find('.musictitlewrap .publishtextmusic').html();
jQuery('.musicmaintitletop').text(currentmusictitle);
jQuery('.publishtextmusictop').html(currentmusicpublisheddate);
jwplayer().play();
});
});

I need some dynamic way of stopping all players and playing the clicked one.

Thanks in advance.

11 Community Answers

contact N/A

User  
0 rated :

Any feedback please. It is very urgent

Todd

JW Player Support Agent  
0 rated :

How many players on the page? You can tell each one to stop individually with:

jwplayer(‘playerID’).play(false);

To iterate through this list dynamically, you would need a function to grab all the jwplayers on the page and iterate through the results:

for (i=0; i<document.getElementsByClassName('jwplayer').length;i++) { 
  jwplayer(document.getElementsByClassName('jwplayer')[i]).play(false);
}

Anahit

User  
0 rated :

Just now i have 3 but I need to pause all the rest dynamically. Where i need to add this code? Do i need to add extra code to this so all rest players will stop and the current clicked one only will play?

Thanks in advance.

Anahit

User  
0 rated :

I added your code and it is working perfectly but now i don't know how to autoplay the current video?

Any idea please?

I tried jwplayer().play() but it is not working properly.

Todd

JW Player Support Agent  
0 rated :

You would need to know the player ID of the current video and then ignore that player when you go through your results. I am certainly not an expert when it comes to Javascript, but I would do something like this:

<div id="player1"></div><br><br>
<div id="player2"></div><br><br>
<div id="player3"></div><br><br>
<div id="player4"></div><br><br>
<script>	
jwplayer('player1').setup({
	file: 'bunny.mp4',
	image: 'bunny.jpg'
});
jwplayer('player2').setup({
	file: 'tears.mp4',
	image: 'tears.jpg'
});
jwplayer('player3').setup({
	file: 'sintel.mp4',
	image: 'sintel.jpg'
});
jwplayer('player4').setup({
	file: 'tears.mp4',
	image: 'tears.jpg'
});

jwplayer('player1').on('play',function(){
	playOnlyOne('player1');
});
jwplayer('player2').on('play',function(){
	playOnlyOne('player2');
});
jwplayer('player3').on('play',function(){
	playOnlyOne('player3');
});
jwplayer('player4').on('play',function(){
	playOnlyOne('player4');
});

function playOnlyOne(playThis) {
	for (i=0; i<document.getElementsByClassName('jwplayer').length;i++) { 
		if (document.getElementsByClassName('jwplayer')[i].id != playThis) {
			jwplayer(document.getElementsByClassName('jwplayer')[i]).play(false);		
		}
	}
}
</script>

Anahit

User  
0 rated :

Thank you for the answer.

Is there a way of doing this dynamically as i don't know how many players I will have?

Todd

JW Player Support Agent  
0 rated :

You can attach the .on(‘play’) function directly to the setup call. You would still need to manually insert the playerID into the playOnlyOne() function, but it is the same player ID used in the setup() call:

<body>
<div id="player1"></div><br><br>
<div id="player2"></div><br><br>
<div id="player3"></div><br><br>
<div id="player4"></div><br><br>
<script>	
jwplayer('player1').setup({
	file: 'bunny.mp4',
	image: 'bunny.jpg',
}).on('play',function(){
	playOnlyOne('player1');
});
jwplayer('player2').setup({
	file: 'tears.mp4',
	image: 'tears.jpg'
}).on('play',function(){
	playOnlyOne('player2');
});
jwplayer('player3').setup({
	file: 'sintel.mp4',
	image: 'sintel.jpg'
}).on('play',function(){
	playOnlyOne('player3');
});;
jwplayer('player4').setup({
	file: 'tears.mp4',
	image: 'tears.jpg'
}).on('play',function(){
	playOnlyOne('player4');
});

function playOnlyOne(playThis) {
	console.log("Playing: "+playThis )
	for (i=0; i<document.getElementsByClassName('jwplayer').length;i++) { 
		if (document.getElementsByClassName('jwplayer')[i].id != playThis) {
			jwplayer(document.getElementsByClassName('jwplayer')[i]).play(false);		
		}
	}
}
</script>

Anahit

User  
0 rated :

Thank you for the code I added it.

But again It did not auto play automatically the current video on click.

How I can auto play the current clicked video on some button click?

Thanks in advance.

Todd

JW Player Support Agent  
0 rated :

This code is working for me in Chrome on Mac. When I click play on any of the players, the other videos stop (or do not start).

What browser are you using? Can you send me a link to where you have used this code on a test page?

Brad

User  
0 rated :

Was this ever resolved? It would be awesome to see this working.

Todd

JW Player Support Agent  
0 rated :

Hi Brad,

Did the last code example I provided work for you?

-Todd

This question has received the maximum number of answers.