Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var videos = {
  2.     a: Popcorn("#a"),
  3.     b: Popcorn("#b"),
  4.   },
  5.   scrub = $("#scrub"),
  6.   loadCount = 0,
  7.   events = "play pause timeupdate seeking".split(/\s+/g);
  8.  
  9. // iterate both media sources
  10. Popcorn.forEach(videos, function(media, type) {
  11.  
  12.   // when each is ready...
  13.   media.on("canplayall", function() {
  14.  
  15.     // trigger a custom "sync" event
  16.     this.emit("sync");
  17.  
  18.     // set the max value of the "scrubber"
  19.     scrub.attr("max", this.duration());
  20.  
  21.     // Listen for the custom sync event...    
  22.   }).on("sync", function() {
  23.  
  24.     // Once both items are loaded, sync events
  25.     if (++loadCount == 2) {
  26.  
  27.       // Iterate all events and trigger them on the video B
  28.       // whenever they occur on the video A
  29.       events.forEach(function(event) {
  30.  
  31.         videos.a.on(event, function() {
  32.  
  33.           // Avoid overkill events, trigger timeupdate manually
  34.           if (event === "timeupdate") {
  35.  
  36.             if (!this.media.paused) {
  37.               return;
  38.             }
  39.             videos.b.emit("timeupdate");
  40.  
  41.             // update scrubber
  42.             scrub.val(this.currentTime());
  43.  
  44.             return;
  45.           }
  46.  
  47.           if (event === "seeking") {
  48.             videos.b.currentTime(this.currentTime());
  49.           }
  50.  
  51.           if (event === "play" || event === "pause") {
  52.             videos.b[event]();
  53.           }
  54.         });
  55.       });
  56.     }
  57.   });
  58. });
  59.  
  60. scrub.bind("change", function() {
  61.   var val = this.value;
  62.   videos.a.currentTime(val);
  63.   videos.b.currentTime(val);
  64. });
  65.  
  66. // With requestAnimationFrame, we can ensure that as
  67. // frequently as the browser would allow,
  68. // the video is resync'ed.
  69. function sync() {
  70.   if (videos.b.media.readyState === 4) {
  71.     videos.b.currentTime(
  72.       videos.a.currentTime()
  73.     );
  74.   }
  75.   requestAnimationFrame(sync);
  76. }
  77.  
  78. sync();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement