3

Has anyone used YouTube iFrame API?
I do ask first here before going on SO.

I am trying to play a YouTube video in sync from 2 different computers.

Luckily YouTube iFrameAPI has an event called `onStateChange` that is fired every time a video is paused, played, stopped etc.

This is the scenario...

1. Host creates a session and sends the link to guest.
2. Guest connects with the host.
3. Host plays/pauses/goes to specific time in the vide the video. The video is synced on guest session.

Now I have to figure out how to sync when Guest does an action. The thing is, every time an event is triggered in Host, it sends the command to Guest. The guest obeys BUT THEN the event `onStateChange` is triggered on guest and sends the command to Host. It is an infinite loop that I cannot seem to figure out if the onStateChange is triggered from API or from User interaction.

What I have tried so far...

1. Global variables. No luck.
2. Disable the event handler when the guest is gets data from host and after it finishes syncing, activate the event handler. But the handler still triggers.
3. Timeframe. (an ugly one) . Checks when the last time that event was triggered. If it was less than 1.5 seconds (or other second), it does not send the commands to host.

Comments
  • 0
    Fucking hell.

    Now I will try to to create a server-side script that generates grabs YouTube's "Hotlink" and returns to host and guest.
    And then implement VideoJS library into it.

    I will try it, hopefully it will work.
  • 0
    Check if the the event matches the last command
  • 0
    @terraria99 The thing is that when you change the time in the video, let's say you fast forward from 2:00 to 15:00 the event is executed 3 times.

    1. Pause
    2. Buffer
    3. Play

    One at a time.

    And it does not work.
  • 0
    @ColdFore can you implement the progress bar yourself (outside the iframe)? (long time not working with youtube api, i dont know if it gives you video length...)
    But if you can, then you can also implement the play button (just make a transparent overlay over iframe) and then YOU get ALL the user events, and send them to both iframes (Local and remote) simultanously. That might be worth a try.
  • 0
    @ColdFore because the events are a real bitch, i made that experience already 😂
  • 0
    What about checking if the videos are in sync before trying to sync them.
Your Job Suck?
Get a Better Job
Add Comment