seekTo is better when the video is already playing, such as when you have a custom scrubber that someone can use.
start/end seems to be better as it more clearly informs the person looking at the code (you and me), what is supposed to occur.
No, on reflection, seekTo is the better solution for you there for the code clearly says that when you get to the end, go back to the desired start section.
That’s commonly called an A-B loop on video players, where it loops continuously from A to B.
First we adjust the onPlayerStateChange function, because state is not a value like on or off, but is instead an event object that contains all sorts of interesting things.
On investigating the function parameter, I see that it has data and target, where the target is the player itself. Event is a good name that the youtube API uses, so we’ll go back to using that, and can get the player from the event.
// function onPlayerStateChange(state) {
function onPlayerStateChange(event) {
const player = event.target;
// if (state.data === YT.PlayerState.ENDED) {
if (event.data === YT.PlayerState.ENDED) {
player.seekTo(startSeconds);
}
}
Something similar can happen to the onPlayerReady function too.
function onPlayerReady(event) {
// player = event.target;
const player = event.target;
player.setVolume(50); // percent
}
No, there’s no need for that, I think I was overzelous with that earlier recommendation there. It’s quite clear from things like YT.PlayerState and other surrounding context that it’s the youtube player. Using player as a variable is all that’s needed here.