]> khamidou — GitHub mirrors - popcorn-js.git/commitdiff
adding in scotts stuff
authorAnna Sobiepanek <annaopensource@gmail.com>
Thu, 17 Jun 2010 21:33:44 +0000 (17:33 -0400)
committerAnna Sobiepanek <annaopensource@gmail.com>
Thu, 17 Jun 2010 21:33:44 +0000 (17:33 -0400)
index.html
popcorn.js [new file with mode: 0644]
video/years.ogv [deleted file]
xml/webMadeMovies.xml [new file with mode: 0644]

index 659e9da5352743773afa3dbcd9e8a80b4abe357f..b95be8b54fb62c35bc9bc4a5ff53656ab82a007e 100644 (file)
@@ -1,67 +1,45 @@
 <html> 
-       <head> 
-               <title>html5 video demo</title> 
-               <script src="http://code.jquery.com/jquery.js"></script> 
-               <script src="http://google.com/jsapi"></script> 
-               <script>google.load("language","1");</script>                   
-               <script src="video.js"></script>        
-               <style type="text/css" media="screen"> 
-                        body { background-color:#eee; }
-                        #container {
-                                width:850px;
-                                margin:auto 50px;
-                                border:1px solid #ccc;
-                                background:white;
-                                padding:20px;
-                                position:relative;
-                        }
-                       #sub { 
-                               position:absolute;
-                               top:435px;
-                               left:40px;
-                               color:white;
-                               font-weight:bold;
-                               font-family:sans-serif; 
-                               text-shadow:black 1px 1px 3px;
-                               width:820px;    
-                               font-size:25px;
-                       }
-                        select { font-size:30px; float:left; }
-                        #credit { font-size:25px; color:#999; float:right; padding-top:3px; }
-                                #credit a { color:#555; }
-                        .clear { clear:both; }
-               </style> 
-       </head> 
-       <body> 
-                <div id="container">
-
-                        <video src="video/years.ogv" ontimeupdate="update(this)" controls> 
-                                <subtitles> 
-                                        <subtitle from="0:02" to="0:05">Intro. Lyrics start at ~0:16</subtitle> 
-                                        <subtitle from="0:16" to="0:22.5">I'm fifteen for a moment, caught in between ten and twenty</subtitle> 
-                                        <subtitle from="0:23" to="0:30">and I'm just dreaming, counting the ways to where you are.</subtitle> 
-                                        <subtitle from="0:32" to="0:39">I'm twenty-two for a moment, and she feels better than ever and we're</subtitle> 
-                                        <subtitle from="0:39" to="0:46.5">on fire, making our way back from Mars.</subtitle> 
-                                        <subtitle from="0:48" to="0:56">Fifteen there's still time for you, time to buy and time to lose.</subtitle> 
-                                        <subtitle from="0:56.5" to="1:08">Fifteen, there's never a wish better than this, when you've only got a hundred years to live</subtitle> 
-                                </subtitles> 
-                        </video> 
-                        <div id="sub" border="2"></div><br/>
-
-                        <div id="bottom">
-                                <select id="language">
-                                        <option val="en">English</option>
-                                        <option val="es">Spanish</option>
-                                        <option val="fr">French</option>
-                                        <option val="de">German</option>
-                                        <option val="ja">Japanese</option>
-
-                                </select>
-                                <div id="credit">with help from <a href="http://google.com/translate">Google Translate</a> and <a href="http://jquery.com">jQuery</a></div>
-                        </div>
-                        <div class="clear"></div>
-                 </div>
-       </body> 
-
+  <head> 
+    <title>html5 video demo</title>    
+    <script src="popcorn.js"></script>
+    <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
+    <style type="text/css" media="screen"> 
+      body { 
+        background-color:#eee; 
+      }
+      #container {
+        width:850px;
+        margin:auto 50px;
+        border:1px solid #ccc;
+        background:white;
+        padding:20px;
+        position:relative;
+      }
+      #sub { 
+        position:absolute;
+        top:435px;
+        left:40px;
+        color:white;
+        font-weight:bold;
+        font-family:sans-serif;        
+        text-shadow:black 1px 1px 3px;
+        width:820px;   
+        font-size:25px;
+      }
+      #map {
+          border: solid 1px #000;
+          width: 200px;
+          height: 200px;
+      }
+    </style> 
+  </head> 
+  <body> 
+    <div id="container">
+      <video src="video/webMadeMovies.ogg" data-timeline-sources="xml/webMadeMovies.xml" controls></video>
+      <div id="sub"></div><br />
+      <div id="tagthisvideo">In this video:<span id="inthisvideo"></span></div><br />
+      <div id="map"></div>
+    </div>
+  </body> 
 </html> 
 
diff --git a/popcorn.js b/popcorn.js
new file mode 100644 (file)
index 0000000..6eed955
--- /dev/null
@@ -0,0 +1,229 @@
+(function() {
+
+  // The video manager manages a single video element, and all it's commands.
+  var VideoManager = this.VideoManager = function(videoElement) {
+    this.commandObjects = {};
+    this.videoElement = videoElement;
+    videoElement.videoManager = this;
+    VideoManager.addInstance(this);
+    videoElement.setAttribute("ontimeupdate", "VideoManager.update(this, this.videoManager);"); 
+  };
+  VideoManager.prototype.addCommand = function(command) {
+    this.commandObjects[command.id] = command;
+  };
+  VideoManager.prototype.removeCommand = function(command) {
+    delete this.commandObjects[command.id];
+  };
+  // Update is called on the video every time it's time changes.
+  VideoManager.update = function(vid, manager) {
+    var t = vid.currentTime;
+    // Loops through all commands in the manager, preloading data, and calling in() or out().
+    var commandObject = {};
+    for (var i in manager.commandObjects) {
+      commandObject = manager.commandObjects[i];
+      if (commandObject.running && (commandObject.params.in > t || commandObject.params.out < t)) {
+        commandObject.running = false;
+        commandObject.out();
+      }
+    }
+    for (var i in manager.commandObjects) {
+      commandObject = manager.commandObjects[i];
+      if (!commandObject.loaded && (commandObject.params.in-5) < t && commandObject.params.out > t) {
+        commandObject.loaded = true;
+        commandObject.preload();
+      }
+      if (!commandObject.running && commandObject.params.in < t && commandObject.params.out > t) {
+        commandObject.running = true;
+        commandObject.in();
+      }
+    }
+  };
+
+  // Store VideoManager instances
+  VideoManager.instances = [];
+  VideoManager.instanceIds = {};
+  VideoManager.addInstance = function(manager) {
+    if (typeof manager.videoElement.id === 'undefined' || !manager.videoElement.id.length) {
+      manager.videoElement.id = "__video" + VideoManager.instances.length;
+    }
+    VideoManager.instanceIds[manager.videoElement.id] = VideoManager.instances.length;
+    VideoManager.instances.push(manager);
+  };
+  VideoManager.getInstanceById = function(name) {
+    return VideoManager.instances[VideoManager.instanceIds[name]];
+  };
+  VideoManager.getInstance = function(index) {
+    return VideoManager.instances[index];
+  };
+
+  // Simple function to convert 0:05 to 0.5 in seconds
+  var to_secs = function(time) {
+    var t = time.split(":");
+    return parseInt(t[0] * 60, 10) + parseInt(t[1], 10);
+  };
+
+  ////////////////////////////////////////////////////////////////////////////
+  // Command objects
+  ///////////////////////////////////////////////////////////////////////////
+
+  // Base class for all commands, SubtitleCommand, MapCommand, etc.
+  var VideoCommand = function(name, params, text) {
+    this.params = {};
+    this.text = text;
+    this.running = false;
+    this.loaded = false;
+    this.in = function() {};
+    this.out = function() {};
+    this.preload = function() {};
+    this.id = name + VideoCommand.count++;
+    for (var i = 0, pl = params.length; i < pl; i++) {
+      for (var j = 0, nl = params[i].length; j < nl; j++) {
+        var key = params[i].item(j).nodeName,
+            data = params[i].item(j).nodeValue;
+        if (key === "in" || key === "out") {
+          this.params[key] = to_secs(data);
+        } else {
+          this.params[key] = data;
+        }
+      }
+    }
+  };
+  VideoCommand.count = 0;
+
+  // Child commands. Uses in() and out() to do time based operations
+  var SubtitleCommand = function(name, params, text) {
+    VideoCommand.call(this, name, params, text);
+    this.in = function() {
+      document.getElementById("sub").innerHTML  = this.text;
+    };
+    this.out = function() {
+      document.getElementById("sub").innerHTML  = "";
+    };
+  };
+
+  var TagCommand = function(name, params, text) {
+    VideoCommand.call(this, name, params, text);
+    this.in = function() {
+      TagCommand.people.contains[this.text] = this.text;
+      document.getElementById("inthisvideo").innerHTML  = TagCommand.people.toString();
+    };
+    this.out = function() {
+      delete TagCommand.people.contains[this.text];
+      document.getElementById("inthisvideo").innerHTML  = TagCommand.people.toString();
+    };
+  };
+  TagCommand.people = {
+    contains: {},
+    toString: function() {
+      var r = [];
+      for (var i in this.contains) {
+        r.push(" " + this.contains[i]);
+      }
+      return r.toString();
+    }
+  };
+
+  var MapCommand = function(name, params, text) {
+    VideoCommand.call(this, name, params, text);
+    this.params.zoom = parseInt(this.params.zoom);
+    // load the map
+    // http://code.google.com/apis/maps/documentation/javascript/reference.html#MapOptions  <-- Map API
+    this.location = new google.maps.LatLng(this.params.lat, this.params.long);
+    if (!MapCommand.map) {
+      MapCommand.map = new google.maps.Map(document.getElementById(this.params.target), {mapTypeId: google.maps.MapTypeId.HYBRID});
+      MapCommand.map.setCenter(new google.maps.LatLng(0, 0))
+      MapCommand.map.setZoom(0);
+    }
+    this.in = function() {
+      MapCommand.map.setCenter(this.location);
+      MapCommand.map.setZoom(this.params.zoom);
+    };
+    this.out = function() {
+      MapCommand.map.setCenter(new google.maps.LatLng(0, 0))
+      MapCommand.map.setZoom(0);
+    };
+  };
+
+  var TwitterCommand = function(name, params, text) {}; // http://twitter.com/celinecelines
+
+  // Wrapper for accessing commands by name
+  // commands[name].create() returns a new command of type name
+  // Not sure if this is the best way; maybe it's too fancy?
+  // I liked it more than a switch, though
+  var commands = {
+    subtitle: {
+      create: function(name, params, text) {
+        return new SubtitleCommand(name, params, text);
+      }
+    },
+    tagThisVideo: {
+      create: function(name, params, text) {
+        return new TagCommand(name, params, text);
+      }
+    },
+    location: {
+      create: function(name, params, text) {
+        return new MapCommand(name, params, text);
+      }
+    }
+  };
+
+  // Parses xml into command objects and adds them to the video manager
+  var parse = function(xmlDoc, videoManager) {
+    var parseNode = function(node, attributes) {
+      var allAttributes = attributes.slice(0);
+      allAttributes.push(node.attributes);
+      var childNodes = node.childNodes;
+      if (childNodes.length < 1 || (childNodes.length === 1 && childNodes[0].nodeType === 3)) {
+        videoManager.addCommand(commands[node.nodeName].create(node.nodeName, allAttributes, node.textContent));
+      } else {
+        for (var i = 0; i < childNodes.length; i++) {
+          childNodes[i].nodeType === 1 && parseNode(childNodes[i], allAttributes);
+        }
+      }
+    };
+
+    for (var j = 0, dl = xmlDoc.length; j < dl; j++) {
+      var x = xmlDoc[j].documentElement.childNodes;
+      for (var i = 0, xl = x.length; i < xl; i++) {
+        x[i].nodeType === 1 && parseNode(x[i], []);
+      }
+    }
+  };
+
+  // Loads an external xml file, and returns the xml object
+  var loadXMLDoc = function(name) {
+    var xhttp = new XMLHttpRequest();
+    if (xhttp) {
+      xhttp.open("GET", name, false);
+      xhttp.send();
+      return xhttp.responseXML;
+    } else {
+      return false;
+    }
+  };
+
+  // Automatic Initialization Method
+  var init = function() {
+    var video = document.getElementsByTagName('video');
+    for (var i = 0, l = video.length; i < l; i++) {
+      var videoSources = video[i].getAttribute('data-timeline-sources');
+      if (videoSources) {
+        var filenames = videoSources.split(' ');
+        var xml = [];
+        for (var j=0, fl=filenames.length; j<fl; j++) {
+          if (filenames[j]) {
+            xml.push(loadXMLDoc(filenames[j]));
+          }
+        }
+        var manager = new VideoManager(video[i]);
+        parse(xml, manager);
+      }
+    }
+  };
+  document.addEventListener('DOMContentLoaded', function() {
+    init();
+  }, false);
+  
+}());
+
diff --git a/video/years.ogv b/video/years.ogv
deleted file mode 100644 (file)
index 766124b..0000000
Binary files a/video/years.ogv and /dev/null differ
diff --git a/xml/webMadeMovies.xml b/xml/webMadeMovies.xml
new file mode 100644 (file)
index 0000000..da74059
--- /dev/null
@@ -0,0 +1,23 @@
+<popcorn>
+  <subtitles>
+    <subtitle in="0:05" out="0:07">Hello! My name is Celine Celine</subtitle>
+    <subtitle in="0:07" out="0:09">And I help the Internet be easier</subtitle>
+    <subtitle in="0:09" out="0:12">by making things easier</subtitle>
+    <subtitle in="0:15" out="0:17">My name is Mona Kasra</subtitle>
+    <subtitle in="0:17" out="0:19">and I'm building a better internet</subtitle>
+  </subtitles>
+  <inThisVideo>
+    <tagThisVideo in="0:03" out="0:14">Celine Celine</tagThisVideo>
+         <tagThisVideo in="0:13" out="0:16">Bryan</tagThisVideo>
+    <tagThisVideo in="0:15" out="0:19">Mona Kasra</tagThisVideo>
+         <tagThisVideo in="0:19" out="0:21">Caleb</tagThisVideo>
+         <tagThisVideo in="0:21" out="0:31">Dave</tagThisVideo>
+         <tagThisVideo in="0:21" out="0:33">Noah</tagThisVideo>
+         <tagThisVideo in="0:39" out="0:41">Celine Celine</tagThisVideo>
+  </inThisVideo>
+  <map>
+    <location id="map1" in="0:01" out="0:30" lat="45.505" long="-73.335" target="map" zoom="8"/>
+    <location id="map2" in="0:30" out="1:00" lat="43.771" long="-79.5" target="map" zoom="16"/>
+    <location id="map3" in="1:00" out="1:30" lat="41.771" long="-76.5" target="map" zoom="16"/>
+  </map>
+</popcorn>