src/widgets/PopcornPlayer.js
changeset 1072 ac1eacb3aa33
parent 1069 2409cb4cebaf
equal deleted inserted replaced
1071:02c04d2c8fd8 1072:ac1eacb3aa33
     1 IriSP.Widgets.PopcornPlayer = function(player, config) {
     1 import jQuery from "jquery";
     2     IriSP.Widgets.Widget.call(this, player, config);
       
     3 };
       
     4 
     2 
     5 IriSP.Widgets.PopcornPlayer.prototype = new IriSP.Widgets.Widget();
     3 const PopcornPlayer = function (ns) {
     6 
     4   return class extends ns.Widgets.Widget {
     7 /* A Popcorn-based player for HTML5 Video, Youtube and Vimeo */
     5     constructor(player, config) {
     8 
     6       super(player, config);
     9 IriSP.Widgets.PopcornPlayer.prototype.defaults = {
       
    10 };
       
    11 
       
    12 IriSP.Widgets.PopcornPlayer.prototype.draw = function() {
       
    13     if (typeof this.video === "undefined") {
       
    14         this.video = this.media.video;
       
    15     }
     7     }
    16 
     8 
    17     if (this.url_transform) {
     9     /* A Popcorn-based player for HTML5 Video, Youtube and Vimeo */
       
    10 
       
    11     static defaults = {};
       
    12 
       
    13     draw() {
       
    14       if (typeof this.video === "undefined") {
       
    15         this.video = this.media.video;
       
    16       }
       
    17 
       
    18       if (this.url_transform) {
    18         this.video = this.url_transform(this.video);
    19         this.video = this.url_transform(this.video);
    19     }
    20       }
    20 
    21 
    21     var _url = this.video;
    22       var _url = this.video;
    22 
    23 
    23     if (/^(https?:\/\/)?(www\.)?youtube\.com/.test(this.video)) {
    24       if (/^(https?:\/\/)?(www\.)?youtube\.com/.test(this.video)) {
    24         /* YOUTUBE */
    25         /* YOUTUBE */
    25         var _urlparts = this.video.split(/[?&]/),
    26         var _urlparts = this.video.split(/[?&]/),
    26             _params = {};
    27           _params = {};
    27         for (var i = 1; i < _urlparts.length; i++) {
    28         for (var i = 1; i < _urlparts.length; i++) {
    28             var _ppart = _urlparts[i].split('=');
    29           var _ppart = _urlparts[i].split("=");
    29             _params[_ppart[0]] = decodeURIComponent(_ppart[1]);
    30           _params[_ppart[0]] = decodeURIComponent(_ppart[1]);
    30         }
    31         }
    31         _params.controls = 0;
    32         _params.controls = 0;
    32         _params.modestbranding = 1;
    33         _params.modestbranding = 1;
    33         if (this.autostart || this.autoplay) {
    34         if (this.autostart || this.autoplay) {
    34             _params.autoplay = 1;
    35           _params.autoplay = 1;
    35         }
    36         }
    36         _url = _urlparts[0] + '?' + IriSP.jQuery.param(_params);
    37         _url = _urlparts[0] + "?" + jQuery.param(_params);
       
    38       } // else {
       
    39       //     /* DEFAULT HTML5 */
       
    40       //     var _tmpId = ns._.uniqueId("popcorn"),
       
    41       //         _videoEl = jQuery('<video>');
       
    42       //     _videoEl.attr({
       
    43       //         id : _tmpId,
       
    44       //         width : this.width,
       
    45       //         height : this.height || undefined
       
    46       //     });
       
    47       //     if(typeof this.video === "string"){
       
    48       //         _videoEl.attr("src",this.video);
       
    49       //     } else {
       
    50       //         for (var i = 0; i < this.video.length; i++) {
       
    51       //             var _srcNode = jQuery('<source>');
       
    52       //             _srcNode.attr({
       
    53       //                 src: this.video[i].src,
       
    54       //                 type: this.video[i].type
       
    55       //             });
       
    56       //             _videoEl.append(_srcNode);
       
    57       //         }
       
    58       //     }
       
    59       //     this.$.html(_videoEl);
       
    60       // }
    37 
    61 
    38     }// else {
    62       var _popcorn = global.Popcorn.smart("#" + this.container, _url);
    39     //     /* DEFAULT HTML5 */
       
    40     //     var _tmpId = IriSP._.uniqueId("popcorn"),
       
    41     //         _videoEl = IriSP.jQuery('<video>');
       
    42     //     _videoEl.attr({
       
    43     //         id : _tmpId,
       
    44     //         width : this.width,
       
    45     //         height : this.height || undefined
       
    46     //     });
       
    47     //     if(typeof this.video === "string"){
       
    48     //         _videoEl.attr("src",this.video);
       
    49     //     } else {
       
    50     //         for (var i = 0; i < this.video.length; i++) {
       
    51     //             var _srcNode = IriSP.jQuery('<source>');
       
    52     //             _srcNode.attr({
       
    53     //                 src: this.video[i].src,
       
    54     //                 type: this.video[i].type
       
    55     //             });
       
    56     //             _videoEl.append(_srcNode);
       
    57     //         }
       
    58     //     }
       
    59     //     this.$.html(_videoEl);
       
    60     // }
       
    61 
    63 
    62     var _popcorn = Popcorn.smart("#"+this.container, _url);
    64       if (this.autostart || this.autoplay) {
       
    65         _popcorn.autoplay(true);
       
    66       }
    63 
    67 
    64     if (this.autostart || this.autoplay) {
    68       var _media = this.media;
    65         _popcorn.autoplay(true);
       
    66     }
       
    67 
    69 
    68     var _media = this.media;
    70       // Binding functions to Popcorn
    69 
    71 
    70     // Binding functions to Popcorn
    72       _media.on("setcurrenttime", function (_milliseconds) {
       
    73         _popcorn.currentTime(_milliseconds / 1000);
       
    74       });
    71 
    75 
    72     _media.on("setcurrenttime", function(_milliseconds) {
    76       _media.on("setvolume", function (_vol) {
    73         _popcorn.currentTime(_milliseconds / 1000);
       
    74     });
       
    75 
       
    76     _media.on("setvolume", function(_vol) {
       
    77         _popcorn.volume(_vol);
    77         _popcorn.volume(_vol);
    78         _media.volume = _vol;
    78         _media.volume = _vol;
    79     });
    79       });
    80 
    80 
    81     _media.on("setmuted", function(_muted) {
    81       _media.on("setmuted", function (_muted) {
    82         _popcorn.muted(_muted);
    82         _popcorn.muted(_muted);
    83         _media.muted = _muted;
    83         _media.muted = _muted;
    84     });
    84       });
    85 
    85 
    86     _media.on("setplay", function() {
    86       _media.on("setplay", function () {
    87         _popcorn.play();
    87         _popcorn.play();
    88     });
    88       });
    89 
    89 
    90     _media.on("setpause", function() {
    90       _media.on("setpause", function () {
    91         _popcorn.pause();
    91         _popcorn.pause();
    92     });
    92       });
    93     _media.on("settimerange", function(_timeRange){
    93       _media.on("settimerange", function (_timeRange) {
    94         _media.timeRange = _timeRange;
    94         _media.timeRange = _timeRange;
    95         try {
    95         try {
    96             if (_media.getCurrentTime() > _timeRange[0] || _media.getCurrentTime() < _timeRange){
    96           if (
    97                 _popcorn.currentTime(_timeRange[0] / 1000);
    97             _media.getCurrentTime() > _timeRange[0] ||
    98             }
    98             _media.getCurrentTime() < _timeRange
    99         } catch (err) {
    99           ) {
   100         }
   100             _popcorn.currentTime(_timeRange[0] / 1000);
   101     })
   101           }
   102     _media.on("resettimerange", function(){
   102         } catch (err) {}
       
   103       });
       
   104       _media.on("resettimerange", function () {
   103         _media.timeRange = false;
   105         _media.timeRange = false;
   104     })
   106       });
   105     // Binding Popcorn events to media
   107       // Binding Popcorn events to media
   106 
   108 
   107     function getVolume() {
   109       function getVolume() {
   108         _media.muted = _popcorn.muted();
   110         _media.muted = _popcorn.muted();
   109         _media.volume = _popcorn.volume();
   111         _media.volume = _popcorn.volume();
   110     }
   112       }
   111 
   113 
   112     _popcorn.on("loadedmetadata", function() {
   114       _popcorn.on("loadedmetadata", function () {
   113         getVolume();
   115         getVolume();
   114         _media.trigger("loadedmetadata");
   116         _media.trigger("loadedmetadata");
   115         _media.trigger("volumechange");
   117         _media.trigger("volumechange");
   116     });
   118       });
   117 
   119 
   118     _popcorn.on("timeupdate", function() {
   120       _popcorn.on("timeupdate", function () {
   119         _media.trigger("timeupdate", new IriSP.Model.Time(1000*_popcorn.currentTime()));
   121         _media.trigger(
   120     });
   122           "timeupdate",
       
   123           new ns.Model.Time(1000 * _popcorn.currentTime())
       
   124         );
       
   125       });
   121 
   126 
   122     _popcorn.on("volumechange", function() {
   127       _popcorn.on("volumechange", function () {
   123         getVolume();
   128         getVolume();
   124         _media.trigger("volumechange");
   129         _media.trigger("volumechange");
   125     });
   130       });
   126 
   131 
   127     _popcorn.on("play", function(e) {
   132       _popcorn.on("play", function (e) {
   128         _media.trigger("play");
   133         _media.trigger("play");
   129     });
   134       });
   130 
   135 
   131     _popcorn.on("pause", function() {
   136       _popcorn.on("pause", function () {
   132         _media.trigger("pause");
   137         _media.trigger("pause");
   133     });
   138       });
   134 
   139 
   135     _popcorn.on("seeked", function() {
   140       _popcorn.on("seeked", function () {
   136         _media.trigger("seeked");
   141         _media.trigger("seeked");
   137     });
   142       });
       
   143     }
       
   144   };
       
   145 };
   138 
   146 
   139 };
   147 export { PopcornPlayer };