This is the type of code I’m looking to implement:
Without the jquery:
players = new Array();
function onYouTubeIframeAPIReady() {
var temp = $("iframe.yt_players");
for (var i = 0; i < temp.length; i++) {
var t = new YT.Player($(temp[i]).attr('id'), {
events: {
'onStateChange': onPlayerStateChange,
'onReady': onPlayerReady,
}
});
players.push(t);
}
}
onYouTubeIframeAPIReady();
function onPlayerStateChange(event) {
if (event.data == YT.PlayerState.PLAYING) {
var temp = event.target.a.src;
var tempPlayers = $("iframe.yt_players");
for (var i = 0; i < players.length; i++) {
if (players[i].a.src != temp)
players[i].pauseVideo();
}
}
}
This one works like that too:
When clicking on another video, the one before it pauses.
This does seem like it would be simple to implement.
But I don’t know how I would set it up.
function onPlayerStateChange(event) {
if (event.data == YT.PlayerState.PLAYING) {
var temp = event.target.getVideoUrl();
var tempPlayers = $("iframe.yt_players");
for (var i = 0; i < players.length; i++) {
if (players[i].getVideoUrl() != temp) players[i].pauseVideo();
}
}
}
Instead of event.target.a.src;
function onPlayerStateChange(event) {
if (event.data == YT.PlayerState.PLAYING) {
var temp = event.target.a.src;
var tempPlayers = $("iframe.yt_players");
for (var i = 0; i < players.length; i++) {
if (players[i].a.src != temp)
players[i].pauseVideo();
}
}
}
I don’t know which it should be.
And is there a difference in which is used?
Maybe one is more appropriate to use than the other for this.
It looks like you are trying many different things that are related and similar, but aren’t quite what you actually require when it comes to looping some of the videos but not others.
Tomorrow I’ll have a go at everything you’ve come up with, and supply meaningful information about each of them.
Yes, that works in the following with a simple addition.
Once you have the youtube event you can get the player object, which is already being done.
From that player object you can get playerVars from player.b.b.playerVars
I looked through the available player function methods and didn’t find any that would give us playerVars any other way. It’s preferable to use built-in function methods but failing that, we can get it directly from player.b.b.playerVars
From there you can get the start value. With that there’s no need for the startSeconds or endSeconds parts of the code.
The only trouble there is customizing the code so that it knows if you want to loop or not. That can be easily achieved by adding a playerVars key/value pair of loop: true - that way you can check if playerVars.loop is true before seeking back to the start.
Yes, that is stopping all other video players when you start another one. I recommend that you focus on one thing at a time to help avoid confusion.
Using the built-in functions such as getVideoUrl() is preferable as that helps to protect you from from API changes in the future.
What do you want to work on first is the primary question here - looping playback or solo play?
Okay then. You don’t need startSeconds or endSeconds, because those can be already defined when you init the player. From inside of the onPlayerStateChange function you can access playerVars via player.b.b.playerVars, and from there get to the start/end information.