Background
I am trying to integrate Videos into a slide show presentation app. I have disabled the controls on the students side and have a play/pause button wired up to the YouTube API so that when it is clicked an event is triggered through pusher and the Video starts on the teachers screen and the Students screen.
Difficulties
The problem comes when one of the users has a slow internet connection. If The teacher has faster internet than the student then the videos will be out of sync.
My Approach
I have a function which uses the YouTube API to control the playing and pausing of a video.
/**
*
*/
togglePlay: function(){
var videoId = this.videoId;
if (this.isPlaying) {
this.players[videoId].pauseVideo(); // Pauses the video
this.isPlaying = false;
} else {
this.players[videoId].playVideo(); // Starts the video
this.isPlaying = true;
}
},
Inside the video object I have added an onStateChange
event listener that is triggered when a video is playing/paused/buffering. The event is sent to this function.
/**
*
*/
emittStateChange: function(e){
switch(e.data) {
// if the teachers video is...
case 1: // playing
pusher.channel4.trigger('client-youtube-triggered', {
videoId: youtube.videoId,
status: 1, // start the students video
});
break;
case 2: // paused
pusher.channel4.trigger('client-youtube-triggered', {
videoId: youtube.videoId,
status: 2, // pause the students video
});
break;
case 3: // buffering
pusher.channel4.trigger('client-youtube-triggered', {
videoId: youtube.videoId,
status: 2, // pause the students video
});
}
},
So when the teachers presses play the video starts and truggers a play event which gets sent to the above function. This function sends a pusher event to the students browser with a status of 1 which means play the video. This event is received by this function:
/**
*
*/
receiveStateChange: function(data){
this.videoId = data.videoId;
switch(data.status) {
// if the teachers video is...
case 1: // playing
this.isPlaying = false;
this.togglePlay();
break;
case 2: // paused
this.isPlaying = true;
this.togglePlay();
break;
case 2: // buffering
this.isPlaying = true;
this.togglePlay();
}
},
My understanding
togglePlay()
is calledthis.isPlaying = false
so playVideo()
is calledonStateChange
eventemittStateChange()
function with status 1
(playing)receiveStateChange()
functionthis.isPlaying()
is set to falseProblem:
What I want:
When the students video is buffering I just want to temporarily pause the teachers video UNTIL the students is playing then play both. I just don't understand how I should break this cycle that ends in both videos being paused.
I am sure you can send more than one event at a time.
So you can send info to the teacher about which student is "buffering" and any other relevant info.
Then on the teachers side, set up a conditional statement that will evaluate the message. So that in the function, the teachers player will not send a control instruction to the student if that condition exists.
As well as have a bypass on the teachers side that if they think it needed, they can stop/pause the video at any time on all players.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With