Advertisement
Guest User

Untitled

a guest
Sep 20th, 2012
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. * slideCastController.js
  3. * © by pschne2s, nkopp2s, 2012
  4. *
  5. * Basically encapsulates the Media Player Object, handling the function of the HTML5-Player
  6. */
  7.  
  8. /****************************OBJECT DEFINITION***********************************/
  9.  
  10. CanvasRenderingContext2D.prototype.clear = CanvasRenderingContext2D.prototype.clear ||
  11. function(preserveTransform) {
  12.     if (preserveTransform) {
  13.         this.save();
  14.         this.setTransform(1, 0, 0, 1, 0, 0);
  15.     }
  16.  
  17.     this.clearRect(0, 0, this.canvas.width, this.canvas.height);
  18.  
  19.     if (preserveTransform) {
  20.         this.restore();
  21.     }
  22. };
  23.  
  24. var mediaPlayer = (function() {
  25.     //Members
  26.     var _cueIdx = 0;
  27.     var _cues = [];
  28.     var _target;
  29.     var _track;
  30.     var _readyToPlay = false;
  31.     var _currImgSrc;
  32.  
  33.     function setupCues() {
  34.         if (_readyToPlay) {
  35.             if (_track === undefined) {
  36.                 // var trackElements = document.getElementById("audioTrack");
  37.                 var trackElements = $(_target).children("track")[0];
  38.                 _track = trackElements.track;
  39.             }
  40.             _cues = _track.cues;
  41.  
  42.             $("#maxcues").text(_cues.length - 1);
  43.             for (var j = 0; j < _cues.length; ++j) {
  44.                 var cue = _cues[j];
  45.                 cue.onenter = getOnEnter(j);
  46.             }
  47.  
  48.             _track.oncuechange = function() {
  49.                 //updateURL(mediaPlayerInfo.cueIdx);
  50.             };
  51.  
  52.             checkFragments();
  53.         } else {
  54.             setTimeout(setupCues, 1);
  55.         }
  56.     };
  57.  
  58.     function getOnEnter(idx) {
  59.         return function() {
  60.             setCueIdx(idx);
  61.             updateURL(idx);
  62.             var json = JSON.parse(this.text);
  63.  
  64.             _currImgSrc = json.src;
  65.             drawIt();
  66.         }
  67.     };
  68.  
  69.     function drawIt() {
  70.         var ctx = $("#slide")[0].getContext('2d');
  71.         ctx.clear();
  72.  
  73.         var tmpImg = new Image();
  74.         tmpImg.onload = function() {
  75.             ctx.drawImage(tmpImg, 0, 0,592,256);
  76.         }
  77.        
  78.         tmpImg.src = _currImgSrc;
  79.  
  80.     }
  81.  
  82.     function checkFragments() {
  83.         var fragments = purl(window.document.URL);
  84.         var slide = fragments.fparam("slide");
  85.         if (slide !== undefined) {
  86.             gotoCue(slide);
  87.         }
  88.         var action = fragments.fparam("action");
  89.         if (action == "play") {
  90.             _target.play();
  91.         }
  92.     };
  93.  
  94.     function setCueIdx(idx) {
  95.         //alert("Cue idx set to: " + idx);
  96.         _cueIdx = idx;
  97.         $("#cueidx").text(idx);
  98.     };
  99.  
  100.     function gotoCue(index) {
  101.         if (index >= 0 && index < _cues.length) {
  102.             _cueIdx = index;
  103.             var audioElement = $("#audiocast").get(0);
  104.             //mediaPlayerInfo.cues[mediaPlayerInfo.cueIdx].onenter();
  105.             audioElement.currentTime = _cues[index].startTime;
  106.         }
  107.     };
  108.  
  109.     var updateURL = (function() {
  110.         // set Base-URL (without query/hash-String)
  111.         var url = location.protocol + "//" + location.host + location.pathname;
  112.         var html5 = window.history.replaceState !== undefined ? true : false;
  113.  
  114.         return function(slideIdx) {
  115.             if (html5)
  116.                 window.history.replaceState(null, document.title + " | Slide #" + slideIdx, url + "#slide=" + slideIdx);
  117.             else
  118.                 location.href = url + "#slide=" + slideIdx;
  119.             // No nice browser history
  120.         };
  121.     })();
  122.  
  123.     return {
  124.         init : function() {
  125.             setupCues();
  126.         },
  127.         setTarget : function(obj) {
  128.             _target = obj;
  129.         },
  130.         setTrack : function(obj) {
  131.             _track = obj;
  132.         },
  133.         setReadyToPlay : function() {
  134.             _readyToPlay = true;
  135.         },
  136.         gotoNextCue : function() {
  137.             gotoCue(_cueIdx + 1);
  138.         },
  139.         gotoPrevCue : function() {
  140.             gotoCue(_cueIdx - 1);
  141.         }
  142.     };
  143. })();
  144.  
  145. $(document).ready(function() {
  146.     // "Unobstrusive" Function-Bindings
  147.     $("#btnPrevCue").bind("click", mediaPlayer.gotoPrevCue);
  148.     $("#btnNextCue").bind("click", mediaPlayer.gotoNextCue);
  149.  
  150.     mediaPlayer.setTarget($("#audiocast")[0]);
  151.     mediaPlayer.init();
  152. });
  153.  
  154. window.setReadyToPlay = mediaPlayer.setReadyToPlay;
  155. // does not work using other ways atm
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement