src/js/pop.js
author veltr
Mon, 17 Sep 2012 00:17:06 +0900
branchplayers-as-widgets
changeset 957 4da0a5740b6c
parent 919 972099304059
permissions -rw-r--r--
Starting 'players-as-widgets' branch
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
957
4da0a5740b6c Starting 'players-as-widgets' branch
veltr
parents: 919
diff changeset
     1
//TODO: Remove Popcorn replacement, replace it by Media Events
4da0a5740b6c Starting 'players-as-widgets' branch
veltr
parents: 919
diff changeset
     2
387
d1fe53ad8d72 initial commit
hamidouk
parents:
diff changeset
     3
/* wrapper that simulates popcorn.js because
d1fe53ad8d72 initial commit
hamidouk
parents:
diff changeset
     4
   popcorn is a bit unstable at the time */
388
30277c1e3d46 replaced most of popcorn's functions.
hamidouk
parents: 387
diff changeset
     5
919
972099304059 Improved HTML Mashup
veltr
parents: 917
diff changeset
     6
/* Popcorn.code replacement has been disabled. It didn't work properly and was not even used  */
972099304059 Improved HTML Mashup
veltr
parents: 917
diff changeset
     7
702
f1225d38c150 new, extendable api.
hamidouk
parents: 699
diff changeset
     8
IriSP.PopcornReplacement = {  
442
ee6594f6d00d play pause that just works (tm)
hamidouk
parents: 441
diff changeset
     9
};
390
9b4e5f606d72 first draft, a little rough around the edges.
hamidouk
parents: 388
diff changeset
    10
702
f1225d38c150 new, extendable api.
hamidouk
parents: 699
diff changeset
    11
/** base class for our popcorn-compatible players.
699
0d6200d801ad refactoring the pop.js code.
hamidouk
parents: 652
diff changeset
    12
 */
702
f1225d38c150 new, extendable api.
hamidouk
parents: 699
diff changeset
    13
IriSP.PopcornReplacement.player = function(container, options) {
919
972099304059 Improved HTML Mashup
veltr
parents: 917
diff changeset
    14
  
972099304059 Improved HTML Mashup
veltr
parents: 917
diff changeset
    15
    this.media = { 
972099304059 Improved HTML Mashup
veltr
parents: 917
diff changeset
    16
        "paused": true,
972099304059 Improved HTML Mashup
veltr
parents: 917
diff changeset
    17
        "muted": false
972099304059 Improved HTML Mashup
veltr
parents: 917
diff changeset
    18
    };
870
2c025db10a10 Migrated playerWidget and started annotationsListWidget
veltr
parents: 841
diff changeset
    19
    
919
972099304059 Improved HTML Mashup
veltr
parents: 917
diff changeset
    20
    this.container = container.replace(/^#/,''); //remove '#' at beginning
972099304059 Improved HTML Mashup
veltr
parents: 917
diff changeset
    21
    this.msgPump = {}; /* dictionnary used to receive and send messages */
972099304059 Improved HTML Mashup
veltr
parents: 917
diff changeset
    22
    this._options = options;
917
eb8677d3a663 HTML 5 Mashup Player
veltr
parents: 904
diff changeset
    23
388
30277c1e3d46 replaced most of popcorn's functions.
hamidouk
parents: 387
diff changeset
    24
};
30277c1e3d46 replaced most of popcorn's functions.
hamidouk
parents: 387
diff changeset
    25
699
0d6200d801ad refactoring the pop.js code.
hamidouk
parents: 652
diff changeset
    26
IriSP.PopcornReplacement.player.prototype.listen = function(msg, callback) {
919
972099304059 Improved HTML Mashup
veltr
parents: 917
diff changeset
    27
    if (!this.msgPump.hasOwnProperty(msg)) {
972099304059 Improved HTML Mashup
veltr
parents: 917
diff changeset
    28
        this.msgPump[msg] = [];
972099304059 Improved HTML Mashup
veltr
parents: 917
diff changeset
    29
    }
972099304059 Improved HTML Mashup
veltr
parents: 917
diff changeset
    30
    this.msgPump[msg].push(callback);
699
0d6200d801ad refactoring the pop.js code.
hamidouk
parents: 652
diff changeset
    31
};
0d6200d801ad refactoring the pop.js code.
hamidouk
parents: 652
diff changeset
    32
917
eb8677d3a663 HTML 5 Mashup Player
veltr
parents: 904
diff changeset
    33
IriSP.PopcornReplacement.player.prototype.on = IriSP.PopcornReplacement.player.prototype.listen;
eb8677d3a663 HTML 5 Mashup Player
veltr
parents: 904
diff changeset
    34
699
0d6200d801ad refactoring the pop.js code.
hamidouk
parents: 652
diff changeset
    35
IriSP.PopcornReplacement.player.prototype.trigger = function(msg, params) {
919
972099304059 Improved HTML Mashup
veltr
parents: 917
diff changeset
    36
    if (!this.msgPump.hasOwnProperty(msg)) {
972099304059 Improved HTML Mashup
veltr
parents: 917
diff changeset
    37
        return;
972099304059 Improved HTML Mashup
veltr
parents: 917
diff changeset
    38
    }
972099304059 Improved HTML Mashup
veltr
parents: 917
diff changeset
    39
    var d = this.msgPump[msg];
972099304059 Improved HTML Mashup
veltr
parents: 917
diff changeset
    40
    for(var i = 0; i < d.length; i++) {
972099304059 Improved HTML Mashup
veltr
parents: 917
diff changeset
    41
        d[i].call(window, params);
972099304059 Improved HTML Mashup
veltr
parents: 917
diff changeset
    42
    }
388
30277c1e3d46 replaced most of popcorn's functions.
hamidouk
parents: 387
diff changeset
    43
};
30277c1e3d46 replaced most of popcorn's functions.
hamidouk
parents: 387
diff changeset
    44
917
eb8677d3a663 HTML 5 Mashup Player
veltr
parents: 904
diff changeset
    45
IriSP.PopcornReplacement.player.prototype.emit = IriSP.PopcornReplacement.player.prototype.trigger;
919
972099304059 Improved HTML Mashup
veltr
parents: 917
diff changeset
    46
/*
702
f1225d38c150 new, extendable api.
hamidouk
parents: 699
diff changeset
    47
IriSP.PopcornReplacement.player.prototype.guid = function(prefix) {
388
30277c1e3d46 replaced most of popcorn's functions.
hamidouk
parents: 387
diff changeset
    48
  var str = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
30277c1e3d46 replaced most of popcorn's functions.
hamidouk
parents: 387
diff changeset
    49
      var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
30277c1e3d46 replaced most of popcorn's functions.
hamidouk
parents: 387
diff changeset
    50
      return v.toString(16);
30277c1e3d46 replaced most of popcorn's functions.
hamidouk
parents: 387
diff changeset
    51
   });
30277c1e3d46 replaced most of popcorn's functions.
hamidouk
parents: 387
diff changeset
    52
30277c1e3d46 replaced most of popcorn's functions.
hamidouk
parents: 387
diff changeset
    53
  return prefix + str;
30277c1e3d46 replaced most of popcorn's functions.
hamidouk
parents: 387
diff changeset
    54
};
30277c1e3d46 replaced most of popcorn's functions.
hamidouk
parents: 387
diff changeset
    55
702
f1225d38c150 new, extendable api.
hamidouk
parents: 699
diff changeset
    56
/** init the api after that flash player has been setup - called by the callback
f1225d38c150 new, extendable api.
hamidouk
parents: 699
diff changeset
    57
    defined by the embedded flash player 
919
972099304059 Improved HTML Mashup
veltr
parents: 917
diff changeset
    58
702
f1225d38c150 new, extendable api.
hamidouk
parents: 699
diff changeset
    59
IriSP.PopcornReplacement.player.prototype.__initApi = function() {
f1225d38c150 new, extendable api.
hamidouk
parents: 699
diff changeset
    60
  this.trigger("loadedmetadata"); // we've done more than loading metadata of course,
463
7ef123b2410f trigger a bunch of signals for popcorn compatibility.
hamidouk
parents: 458
diff changeset
    61
                                                      // but popcorn doesn't need to know more.
702
f1225d38c150 new, extendable api.
hamidouk
parents: 699
diff changeset
    62
  this.media.muted = this.playerFns.getMute();
919
972099304059 Improved HTML Mashup
veltr
parents: 917
diff changeset
    63
  /* some programmed segments are supposed to be run at the beginning 
578
8dd6ebb7a79d fixed .code() bug.
hamidouk
parents: 477
diff changeset
    64
  var i = 0;
702
f1225d38c150 new, extendable api.
hamidouk
parents: 699
diff changeset
    65
  for(i = 0; i < this.__codes.length; i++) {
f1225d38c150 new, extendable api.
hamidouk
parents: 699
diff changeset
    66
    var c = this.__codes[i];
578
8dd6ebb7a79d fixed .code() bug.
hamidouk
parents: 477
diff changeset
    67
    if (0 == c.start) {
8dd6ebb7a79d fixed .code() bug.
hamidouk
parents: 477
diff changeset
    68
      c.onStart();
8dd6ebb7a79d fixed .code() bug.
hamidouk
parents: 477
diff changeset
    69
    }
8dd6ebb7a79d fixed .code() bug.
hamidouk
parents: 477
diff changeset
    70
    
8dd6ebb7a79d fixed .code() bug.
hamidouk
parents: 477
diff changeset
    71
    if (0 == c.end) {
8dd6ebb7a79d fixed .code() bug.
hamidouk
parents: 477
diff changeset
    72
      c.onEnd();
8dd6ebb7a79d fixed .code() bug.
hamidouk
parents: 477
diff changeset
    73
    }
8dd6ebb7a79d fixed .code() bug.
hamidouk
parents: 477
diff changeset
    74
  }
919
972099304059 Improved HTML Mashup
veltr
parents: 917
diff changeset
    75
  
388
30277c1e3d46 replaced most of popcorn's functions.
hamidouk
parents: 387
diff changeset
    76
};
919
972099304059 Improved HTML Mashup
veltr
parents: 917
diff changeset
    77
*/
388
30277c1e3d46 replaced most of popcorn's functions.
hamidouk
parents: 387
diff changeset
    78
702
f1225d38c150 new, extendable api.
hamidouk
parents: 699
diff changeset
    79
IriSP.PopcornReplacement.player.prototype.currentTime = function(time) {
605
e1a6f73038b4 made the listWidget redraw at every timeupdate event (added a small optimization though,
hamidouk
parents: 604
diff changeset
    80
  if (typeof(time) === "undefined") {        
702
f1225d38c150 new, extendable api.
hamidouk
parents: 699
diff changeset
    81
      return this.playerFns.getPosition();            
388
30277c1e3d46 replaced most of popcorn's functions.
hamidouk
parents: 387
diff changeset
    82
  } else {
30277c1e3d46 replaced most of popcorn's functions.
hamidouk
parents: 387
diff changeset
    83
     var currentTime = +time;
723
bbf9914a5f99 fixed bug
hamidouk
parents: 711
diff changeset
    84
     this.playerFns.seek(currentTime);              
604
cc2208986a4d WIP - trying to fix a seeking bug.
hamidouk
parents: 596
diff changeset
    85
     return currentTime;
388
30277c1e3d46 replaced most of popcorn's functions.
hamidouk
parents: 387
diff changeset
    86
  }
30277c1e3d46 replaced most of popcorn's functions.
hamidouk
parents: 387
diff changeset
    87
};
30277c1e3d46 replaced most of popcorn's functions.
hamidouk
parents: 387
diff changeset
    88
702
f1225d38c150 new, extendable api.
hamidouk
parents: 699
diff changeset
    89
IriSP.PopcornReplacement.player.prototype.play = function() {
f1225d38c150 new, extendable api.
hamidouk
parents: 699
diff changeset
    90
  this.media.paused = false;
f1225d38c150 new, extendable api.
hamidouk
parents: 699
diff changeset
    91
  this.trigger("play");
f1225d38c150 new, extendable api.
hamidouk
parents: 699
diff changeset
    92
  this.playerFns.play();
388
30277c1e3d46 replaced most of popcorn's functions.
hamidouk
parents: 387
diff changeset
    93
};
30277c1e3d46 replaced most of popcorn's functions.
hamidouk
parents: 387
diff changeset
    94
    
702
f1225d38c150 new, extendable api.
hamidouk
parents: 699
diff changeset
    95
IriSP.PopcornReplacement.player.prototype.pause = function() {
f1225d38c150 new, extendable api.
hamidouk
parents: 699
diff changeset
    96
    this.media.paused = true;
919
972099304059 Improved HTML Mashup
veltr
parents: 917
diff changeset
    97
    this.trigger("pause");
702
f1225d38c150 new, extendable api.
hamidouk
parents: 699
diff changeset
    98
    this.playerFns.pause();
390
9b4e5f606d72 first draft, a little rough around the edges.
hamidouk
parents: 388
diff changeset
    99
};
9b4e5f606d72 first draft, a little rough around the edges.
hamidouk
parents: 388
diff changeset
   100
702
f1225d38c150 new, extendable api.
hamidouk
parents: 699
diff changeset
   101
IriSP.PopcornReplacement.player.prototype.muted = function(val) {
f1225d38c150 new, extendable api.
hamidouk
parents: 699
diff changeset
   102
  if (typeof(val) !== "undefined") {
f1225d38c150 new, extendable api.
hamidouk
parents: 699
diff changeset
   103
f1225d38c150 new, extendable api.
hamidouk
parents: 699
diff changeset
   104
    if (this.playerFns.getMute() !== val) {
f1225d38c150 new, extendable api.
hamidouk
parents: 699
diff changeset
   105
      if (val) {
f1225d38c150 new, extendable api.
hamidouk
parents: 699
diff changeset
   106
        this.playerFns.setMute(true);
f1225d38c150 new, extendable api.
hamidouk
parents: 699
diff changeset
   107
        this.media.muted = true;
f1225d38c150 new, extendable api.
hamidouk
parents: 699
diff changeset
   108
      } else {
f1225d38c150 new, extendable api.
hamidouk
parents: 699
diff changeset
   109
        this.playerFns.setMute(false);
f1225d38c150 new, extendable api.
hamidouk
parents: 699
diff changeset
   110
        this.media.muted = false;
f1225d38c150 new, extendable api.
hamidouk
parents: 699
diff changeset
   111
      }
394
89a79a2c6014 WIP - working on the code function.
hamidouk
parents: 391
diff changeset
   112
702
f1225d38c150 new, extendable api.
hamidouk
parents: 699
diff changeset
   113
      this.trigger( "volumechange" );
f1225d38c150 new, extendable api.
hamidouk
parents: 699
diff changeset
   114
    }
f1225d38c150 new, extendable api.
hamidouk
parents: 699
diff changeset
   115
    
f1225d38c150 new, extendable api.
hamidouk
parents: 699
diff changeset
   116
    return this.playerFns.getMute();
f1225d38c150 new, extendable api.
hamidouk
parents: 699
diff changeset
   117
  } else {
f1225d38c150 new, extendable api.
hamidouk
parents: 699
diff changeset
   118
    return this.playerFns.getMute();
f1225d38c150 new, extendable api.
hamidouk
parents: 699
diff changeset
   119
  }
f1225d38c150 new, extendable api.
hamidouk
parents: 699
diff changeset
   120
};
f1225d38c150 new, extendable api.
hamidouk
parents: 699
diff changeset
   121
839
4357aac4eb19 Added volume support
veltr
parents: 785
diff changeset
   122
IriSP.PopcornReplacement.player.prototype.volume = function(val) {
4357aac4eb19 Added volume support
veltr
parents: 785
diff changeset
   123
    if (typeof this.playerFns.getVolume == "undefined" || typeof this.playerFns.setVolume == "undefined") {
4357aac4eb19 Added volume support
veltr
parents: 785
diff changeset
   124
        return false;
4357aac4eb19 Added volume support
veltr
parents: 785
diff changeset
   125
    }
4357aac4eb19 Added volume support
veltr
parents: 785
diff changeset
   126
    var _vol = this.playerFns.getVolume();
4357aac4eb19 Added volume support
veltr
parents: 785
diff changeset
   127
    if (typeof(val) !== "undefined" && parseFloat(val) !== NaN) {
4357aac4eb19 Added volume support
veltr
parents: 785
diff changeset
   128
        val = Math.max(0, Math.min(1, val));
4357aac4eb19 Added volume support
veltr
parents: 785
diff changeset
   129
        if (parseFloat(val) != parseFloat(_vol)) {
4357aac4eb19 Added volume support
veltr
parents: 785
diff changeset
   130
            this.playerFns.setVolume(val);
4357aac4eb19 Added volume support
veltr
parents: 785
diff changeset
   131
            this.trigger("volumechange");
4357aac4eb19 Added volume support
veltr
parents: 785
diff changeset
   132
            _vol = this.playerFns.getVolume();
4357aac4eb19 Added volume support
veltr
parents: 785
diff changeset
   133
        }
4357aac4eb19 Added volume support
veltr
parents: 785
diff changeset
   134
    }
4357aac4eb19 Added volume support
veltr
parents: 785
diff changeset
   135
    return _vol;
4357aac4eb19 Added volume support
veltr
parents: 785
diff changeset
   136
};
4357aac4eb19 Added volume support
veltr
parents: 785
diff changeset
   137
917
eb8677d3a663 HTML 5 Mashup Player
veltr
parents: 904
diff changeset
   138
IriSP.PopcornReplacement.player.prototype.mute = function() {
eb8677d3a663 HTML 5 Mashup Player
veltr
parents: 904
diff changeset
   139
    this.muted(true);
eb8677d3a663 HTML 5 Mashup Player
veltr
parents: 904
diff changeset
   140
}
eb8677d3a663 HTML 5 Mashup Player
veltr
parents: 904
diff changeset
   141
eb8677d3a663 HTML 5 Mashup Player
veltr
parents: 904
diff changeset
   142
IriSP.PopcornReplacement.player.prototype.unmute = function() {
eb8677d3a663 HTML 5 Mashup Player
veltr
parents: 904
diff changeset
   143
    this.muted(false);
eb8677d3a663 HTML 5 Mashup Player
veltr
parents: 904
diff changeset
   144
}
702
f1225d38c150 new, extendable api.
hamidouk
parents: 699
diff changeset
   145
441
99b7c5192330 fixed horrible bug involving jQuery message passing.
hamidouk
parents: 431
diff changeset
   146
702
f1225d38c150 new, extendable api.
hamidouk
parents: 699
diff changeset
   147
IriSP.PopcornReplacement.player.prototype.roundTime = function() {
f1225d38c150 new, extendable api.
hamidouk
parents: 699
diff changeset
   148
  var currentTime = this.currentTime();
394
89a79a2c6014 WIP - working on the code function.
hamidouk
parents: 391
diff changeset
   149
  return Math.round(currentTime);
711
323205a7bd39 separated jwplayer code from pop.js and put it into its own folder, players/.
hamidouk
parents: 706
diff changeset
   150
};