I am working on a application using Youtube Iframe Javascript API. I have to play or pause the video based on the input from another application(Referred as calling application). I also have to get update from this Youtube player(whether it got played or paused) and update the calling application.
Now the question is how to differentiate if the you tube player is played by the user action or based on the input from the Calling application. Because both of them invoke the action listener. When Played by the calling application I dont want the calling application to be updated again.
For example:
Case 1:
- Calling application sends play request to youtube player.
- Youtube player is played.
- onStateChange listener is invoked.
- Calling application is notified that the player state has changed. (I dont want this to happen).
Case 2:
- User hits play button on the player.
- Youtube player is played.
- onStateChange listener is invoked.
- Calling application is notified that the player state has changed. (I want this to happen).
I can do this with a switch variable when the program invokes it and disable it when the user invokes it. But there are too many such situations and I can't expect all the developers to handle it considering all these situations.
So I am in need of a proper way for the action listener to know if the user had invoked that action or a program has invoked the action. Is there a built in way in the API to do this?
function callback(responseText) {
var response=JSON.parse(responseText.body);
var player = getPlayer();
var seekAhead = true;
switch (response.playerState) {
case YT.PlayerState.PLAYING:
player.seekTo(response.seconds,seekAhead);
player.playVideo();
break;
case YT.PlayerState.PAUSED:
player.seekTo(response.seconds,seekAhead);
player.pauseVideo();
break;
case YT.PlayerState.BUFFERING:
break;
case YT.PlayerState.ENDED:
break;
}
The problem with using a variable to manage the switch is that, when the Calling application sends a play state, there is a seekTo call which invokes pause, buffer and play events. They take a while to complete but the variable switch gets switched at the first event.