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
- Teacher presses play button
togglePlay()
is calledthis.isPlaying = false
soplayVideo()
is called- video starts playing for teacher
- The YouTube api then triggers an
onStateChange
event - this is sent to the
emittStateChange()
function with status 1 (playing) - This status is sent via pusher to the students
receiveStateChange()
function - for case = 1
this.isPlaying()
is set to false - toggle play is called to start the students video
- Students video starts buffering which triggers the youtube api onStateChange event again
- Status 3 (buffering) is then sent back to the teacher via pusher
- This pauses the teachers video to wait for the student
Problem:
- When the teachers video stops the onStateChange event is triggered yet again before the students video is done buffering and sent to the student.
- This stops the students video and now both videos are paused.
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.