src/widgets/PopcornPlayer.js
author ymh <ymh.work@gmail.com>
Sun, 12 Nov 2017 22:07:33 +0100
changeset 1071 02c04d2c8fd8
parent 1069 2409cb4cebaf
child 1072 ac1eacb3aa33
permissions -rw-r--r--
Various changes from git version and make autoplayer determine video type on lowercase url
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
958
2aa7fdb0762a Commit before branch switch (for tests)
veltr
parents:
diff changeset
     1
IriSP.Widgets.PopcornPlayer = function(player, config) {
2aa7fdb0762a Commit before branch switch (for tests)
veltr
parents:
diff changeset
     2
    IriSP.Widgets.Widget.call(this, player, config);
2aa7fdb0762a Commit before branch switch (for tests)
veltr
parents:
diff changeset
     3
};
2aa7fdb0762a Commit before branch switch (for tests)
veltr
parents:
diff changeset
     4
2aa7fdb0762a Commit before branch switch (for tests)
veltr
parents:
diff changeset
     5
IriSP.Widgets.PopcornPlayer.prototype = new IriSP.Widgets.Widget();
2aa7fdb0762a Commit before branch switch (for tests)
veltr
parents:
diff changeset
     6
959
ee11ed1b739e Mashup Player and Dailymotion are now widgets
veltr
parents: 958
diff changeset
     7
/* A Popcorn-based player for HTML5 Video, Youtube and Vimeo */
ee11ed1b739e Mashup Player and Dailymotion are now widgets
veltr
parents: 958
diff changeset
     8
958
2aa7fdb0762a Commit before branch switch (for tests)
veltr
parents:
diff changeset
     9
IriSP.Widgets.PopcornPlayer.prototype.defaults = {
1013
392ddcd212d7 Throwed in a bunch of semicolons
veltr
parents: 987
diff changeset
    10
};
958
2aa7fdb0762a Commit before branch switch (for tests)
veltr
parents:
diff changeset
    11
2aa7fdb0762a Commit before branch switch (for tests)
veltr
parents:
diff changeset
    12
IriSP.Widgets.PopcornPlayer.prototype.draw = function() {
2aa7fdb0762a Commit before branch switch (for tests)
veltr
parents:
diff changeset
    13
    if (typeof this.video === "undefined") {
2aa7fdb0762a Commit before branch switch (for tests)
veltr
parents:
diff changeset
    14
        this.video = this.media.video;
2aa7fdb0762a Commit before branch switch (for tests)
veltr
parents:
diff changeset
    15
    }
1068
7623f9af9272 merge pull request #3 from O. Auber
ymh <ymh.work@gmail.com>
parents: 1044
diff changeset
    16
958
2aa7fdb0762a Commit before branch switch (for tests)
veltr
parents:
diff changeset
    17
    if (this.url_transform) {
2aa7fdb0762a Commit before branch switch (for tests)
veltr
parents:
diff changeset
    18
        this.video = this.url_transform(this.video);
2aa7fdb0762a Commit before branch switch (for tests)
veltr
parents:
diff changeset
    19
    }
1068
7623f9af9272 merge pull request #3 from O. Auber
ymh <ymh.work@gmail.com>
parents: 1044
diff changeset
    20
1069
2409cb4cebaf getting various changes from github
ymh <ymh.work@gmail.com>
parents: 1068
diff changeset
    21
    var _url = this.video;
2409cb4cebaf getting various changes from github
ymh <ymh.work@gmail.com>
parents: 1068
diff changeset
    22
2409cb4cebaf getting various changes from github
ymh <ymh.work@gmail.com>
parents: 1068
diff changeset
    23
    if (/^(https?:\/\/)?(www\.)?youtube\.com/.test(this.video)) {
959
ee11ed1b739e Mashup Player and Dailymotion are now widgets
veltr
parents: 958
diff changeset
    24
        /* YOUTUBE */
ee11ed1b739e Mashup Player and Dailymotion are now widgets
veltr
parents: 958
diff changeset
    25
        var _urlparts = this.video.split(/[?&]/),
ee11ed1b739e Mashup Player and Dailymotion are now widgets
veltr
parents: 958
diff changeset
    26
            _params = {};
ee11ed1b739e Mashup Player and Dailymotion are now widgets
veltr
parents: 958
diff changeset
    27
        for (var i = 1; i < _urlparts.length; i++) {
ee11ed1b739e Mashup Player and Dailymotion are now widgets
veltr
parents: 958
diff changeset
    28
            var _ppart = _urlparts[i].split('=');
ee11ed1b739e Mashup Player and Dailymotion are now widgets
veltr
parents: 958
diff changeset
    29
            _params[_ppart[0]] = decodeURIComponent(_ppart[1]);
ee11ed1b739e Mashup Player and Dailymotion are now widgets
veltr
parents: 958
diff changeset
    30
        }
ee11ed1b739e Mashup Player and Dailymotion are now widgets
veltr
parents: 958
diff changeset
    31
        _params.controls = 0;
ee11ed1b739e Mashup Player and Dailymotion are now widgets
veltr
parents: 958
diff changeset
    32
        _params.modestbranding = 1;
967
b4c6e64acb2d Added a dual mode HTML/Flash player
veltr
parents: 959
diff changeset
    33
        if (this.autostart || this.autoplay) {
b4c6e64acb2d Added a dual mode HTML/Flash player
veltr
parents: 959
diff changeset
    34
            _params.autoplay = 1;
b4c6e64acb2d Added a dual mode HTML/Flash player
veltr
parents: 959
diff changeset
    35
        }
959
ee11ed1b739e Mashup Player and Dailymotion are now widgets
veltr
parents: 958
diff changeset
    36
        _url = _urlparts[0] + '?' + IriSP.jQuery.param(_params);
1068
7623f9af9272 merge pull request #3 from O. Auber
ymh <ymh.work@gmail.com>
parents: 1044
diff changeset
    37
1069
2409cb4cebaf getting various changes from github
ymh <ymh.work@gmail.com>
parents: 1068
diff changeset
    38
    }// else {
2409cb4cebaf getting various changes from github
ymh <ymh.work@gmail.com>
parents: 1068
diff changeset
    39
    //     /* DEFAULT HTML5 */
2409cb4cebaf getting various changes from github
ymh <ymh.work@gmail.com>
parents: 1068
diff changeset
    40
    //     var _tmpId = IriSP._.uniqueId("popcorn"),
2409cb4cebaf getting various changes from github
ymh <ymh.work@gmail.com>
parents: 1068
diff changeset
    41
    //         _videoEl = IriSP.jQuery('<video>');
2409cb4cebaf getting various changes from github
ymh <ymh.work@gmail.com>
parents: 1068
diff changeset
    42
    //     _videoEl.attr({
2409cb4cebaf getting various changes from github
ymh <ymh.work@gmail.com>
parents: 1068
diff changeset
    43
    //         id : _tmpId,
2409cb4cebaf getting various changes from github
ymh <ymh.work@gmail.com>
parents: 1068
diff changeset
    44
    //         width : this.width,
2409cb4cebaf getting various changes from github
ymh <ymh.work@gmail.com>
parents: 1068
diff changeset
    45
    //         height : this.height || undefined
2409cb4cebaf getting various changes from github
ymh <ymh.work@gmail.com>
parents: 1068
diff changeset
    46
    //     });
2409cb4cebaf getting various changes from github
ymh <ymh.work@gmail.com>
parents: 1068
diff changeset
    47
    //     if(typeof this.video === "string"){
2409cb4cebaf getting various changes from github
ymh <ymh.work@gmail.com>
parents: 1068
diff changeset
    48
    //         _videoEl.attr("src",this.video);
2409cb4cebaf getting various changes from github
ymh <ymh.work@gmail.com>
parents: 1068
diff changeset
    49
    //     } else {
2409cb4cebaf getting various changes from github
ymh <ymh.work@gmail.com>
parents: 1068
diff changeset
    50
    //         for (var i = 0; i < this.video.length; i++) {
2409cb4cebaf getting various changes from github
ymh <ymh.work@gmail.com>
parents: 1068
diff changeset
    51
    //             var _srcNode = IriSP.jQuery('<source>');
2409cb4cebaf getting various changes from github
ymh <ymh.work@gmail.com>
parents: 1068
diff changeset
    52
    //             _srcNode.attr({
2409cb4cebaf getting various changes from github
ymh <ymh.work@gmail.com>
parents: 1068
diff changeset
    53
    //                 src: this.video[i].src,
2409cb4cebaf getting various changes from github
ymh <ymh.work@gmail.com>
parents: 1068
diff changeset
    54
    //                 type: this.video[i].type
2409cb4cebaf getting various changes from github
ymh <ymh.work@gmail.com>
parents: 1068
diff changeset
    55
    //             });
2409cb4cebaf getting various changes from github
ymh <ymh.work@gmail.com>
parents: 1068
diff changeset
    56
    //             _videoEl.append(_srcNode);
2409cb4cebaf getting various changes from github
ymh <ymh.work@gmail.com>
parents: 1068
diff changeset
    57
    //         }
2409cb4cebaf getting various changes from github
ymh <ymh.work@gmail.com>
parents: 1068
diff changeset
    58
    //     }
2409cb4cebaf getting various changes from github
ymh <ymh.work@gmail.com>
parents: 1068
diff changeset
    59
    //     this.$.html(_videoEl);
2409cb4cebaf getting various changes from github
ymh <ymh.work@gmail.com>
parents: 1068
diff changeset
    60
    // }
1068
7623f9af9272 merge pull request #3 from O. Auber
ymh <ymh.work@gmail.com>
parents: 1044
diff changeset
    61
1069
2409cb4cebaf getting various changes from github
ymh <ymh.work@gmail.com>
parents: 1068
diff changeset
    62
    var _popcorn = Popcorn.smart("#"+this.container, _url);
2409cb4cebaf getting various changes from github
ymh <ymh.work@gmail.com>
parents: 1068
diff changeset
    63
2409cb4cebaf getting various changes from github
ymh <ymh.work@gmail.com>
parents: 1068
diff changeset
    64
    if (this.autostart || this.autoplay) {
2409cb4cebaf getting various changes from github
ymh <ymh.work@gmail.com>
parents: 1068
diff changeset
    65
        _popcorn.autoplay(true);
958
2aa7fdb0762a Commit before branch switch (for tests)
veltr
parents:
diff changeset
    66
    }
1068
7623f9af9272 merge pull request #3 from O. Auber
ymh <ymh.work@gmail.com>
parents: 1044
diff changeset
    67
970
b1c3bf6eca78 Cleaning and refactoring
veltr
parents: 967
diff changeset
    68
    var _media = this.media;
1068
7623f9af9272 merge pull request #3 from O. Auber
ymh <ymh.work@gmail.com>
parents: 1044
diff changeset
    69
958
2aa7fdb0762a Commit before branch switch (for tests)
veltr
parents:
diff changeset
    70
    // Binding functions to Popcorn
1068
7623f9af9272 merge pull request #3 from O. Auber
ymh <ymh.work@gmail.com>
parents: 1044
diff changeset
    71
970
b1c3bf6eca78 Cleaning and refactoring
veltr
parents: 967
diff changeset
    72
    _media.on("setcurrenttime", function(_milliseconds) {
b1c3bf6eca78 Cleaning and refactoring
veltr
parents: 967
diff changeset
    73
        _popcorn.currentTime(_milliseconds / 1000);
b1c3bf6eca78 Cleaning and refactoring
veltr
parents: 967
diff changeset
    74
    });
1068
7623f9af9272 merge pull request #3 from O. Auber
ymh <ymh.work@gmail.com>
parents: 1044
diff changeset
    75
970
b1c3bf6eca78 Cleaning and refactoring
veltr
parents: 967
diff changeset
    76
    _media.on("setvolume", function(_vol) {
b1c3bf6eca78 Cleaning and refactoring
veltr
parents: 967
diff changeset
    77
        _popcorn.volume(_vol);
b1c3bf6eca78 Cleaning and refactoring
veltr
parents: 967
diff changeset
    78
        _media.volume = _vol;
b1c3bf6eca78 Cleaning and refactoring
veltr
parents: 967
diff changeset
    79
    });
1068
7623f9af9272 merge pull request #3 from O. Auber
ymh <ymh.work@gmail.com>
parents: 1044
diff changeset
    80
970
b1c3bf6eca78 Cleaning and refactoring
veltr
parents: 967
diff changeset
    81
    _media.on("setmuted", function(_muted) {
b1c3bf6eca78 Cleaning and refactoring
veltr
parents: 967
diff changeset
    82
        _popcorn.muted(_muted);
b1c3bf6eca78 Cleaning and refactoring
veltr
parents: 967
diff changeset
    83
        _media.muted = _muted;
b1c3bf6eca78 Cleaning and refactoring
veltr
parents: 967
diff changeset
    84
    });
1068
7623f9af9272 merge pull request #3 from O. Auber
ymh <ymh.work@gmail.com>
parents: 1044
diff changeset
    85
970
b1c3bf6eca78 Cleaning and refactoring
veltr
parents: 967
diff changeset
    86
    _media.on("setplay", function() {
b1c3bf6eca78 Cleaning and refactoring
veltr
parents: 967
diff changeset
    87
        _popcorn.play();
b1c3bf6eca78 Cleaning and refactoring
veltr
parents: 967
diff changeset
    88
    });
1068
7623f9af9272 merge pull request #3 from O. Auber
ymh <ymh.work@gmail.com>
parents: 1044
diff changeset
    89
970
b1c3bf6eca78 Cleaning and refactoring
veltr
parents: 967
diff changeset
    90
    _media.on("setpause", function() {
b1c3bf6eca78 Cleaning and refactoring
veltr
parents: 967
diff changeset
    91
        _popcorn.pause();
b1c3bf6eca78 Cleaning and refactoring
veltr
parents: 967
diff changeset
    92
    });
1044
d8339b45edc4 Added support for defining a "time range" for a Playable to set the time boundaries of what can be played by the diffenrent players (Popcorn and HtmlPlayer for now). Added option in Segment widget to define a timerange on click.
durandn
parents: 1013
diff changeset
    93
    _media.on("settimerange", function(_timeRange){
d8339b45edc4 Added support for defining a "time range" for a Playable to set the time boundaries of what can be played by the diffenrent players (Popcorn and HtmlPlayer for now). Added option in Segment widget to define a timerange on click.
durandn
parents: 1013
diff changeset
    94
        _media.timeRange = _timeRange;
d8339b45edc4 Added support for defining a "time range" for a Playable to set the time boundaries of what can be played by the diffenrent players (Popcorn and HtmlPlayer for now). Added option in Segment widget to define a timerange on click.
durandn
parents: 1013
diff changeset
    95
        try {
d8339b45edc4 Added support for defining a "time range" for a Playable to set the time boundaries of what can be played by the diffenrent players (Popcorn and HtmlPlayer for now). Added option in Segment widget to define a timerange on click.
durandn
parents: 1013
diff changeset
    96
            if (_media.getCurrentTime() > _timeRange[0] || _media.getCurrentTime() < _timeRange){
d8339b45edc4 Added support for defining a "time range" for a Playable to set the time boundaries of what can be played by the diffenrent players (Popcorn and HtmlPlayer for now). Added option in Segment widget to define a timerange on click.
durandn
parents: 1013
diff changeset
    97
                _popcorn.currentTime(_timeRange[0] / 1000);
d8339b45edc4 Added support for defining a "time range" for a Playable to set the time boundaries of what can be played by the diffenrent players (Popcorn and HtmlPlayer for now). Added option in Segment widget to define a timerange on click.
durandn
parents: 1013
diff changeset
    98
            }
d8339b45edc4 Added support for defining a "time range" for a Playable to set the time boundaries of what can be played by the diffenrent players (Popcorn and HtmlPlayer for now). Added option in Segment widget to define a timerange on click.
durandn
parents: 1013
diff changeset
    99
        } catch (err) {
d8339b45edc4 Added support for defining a "time range" for a Playable to set the time boundaries of what can be played by the diffenrent players (Popcorn and HtmlPlayer for now). Added option in Segment widget to define a timerange on click.
durandn
parents: 1013
diff changeset
   100
        }
d8339b45edc4 Added support for defining a "time range" for a Playable to set the time boundaries of what can be played by the diffenrent players (Popcorn and HtmlPlayer for now). Added option in Segment widget to define a timerange on click.
durandn
parents: 1013
diff changeset
   101
    })
d8339b45edc4 Added support for defining a "time range" for a Playable to set the time boundaries of what can be played by the diffenrent players (Popcorn and HtmlPlayer for now). Added option in Segment widget to define a timerange on click.
durandn
parents: 1013
diff changeset
   102
    _media.on("resettimerange", function(){
d8339b45edc4 Added support for defining a "time range" for a Playable to set the time boundaries of what can be played by the diffenrent players (Popcorn and HtmlPlayer for now). Added option in Segment widget to define a timerange on click.
durandn
parents: 1013
diff changeset
   103
        _media.timeRange = false;
d8339b45edc4 Added support for defining a "time range" for a Playable to set the time boundaries of what can be played by the diffenrent players (Popcorn and HtmlPlayer for now). Added option in Segment widget to define a timerange on click.
durandn
parents: 1013
diff changeset
   104
    })
958
2aa7fdb0762a Commit before branch switch (for tests)
veltr
parents:
diff changeset
   105
    // Binding Popcorn events to media
1068
7623f9af9272 merge pull request #3 from O. Auber
ymh <ymh.work@gmail.com>
parents: 1044
diff changeset
   106
970
b1c3bf6eca78 Cleaning and refactoring
veltr
parents: 967
diff changeset
   107
    function getVolume() {
b1c3bf6eca78 Cleaning and refactoring
veltr
parents: 967
diff changeset
   108
        _media.muted = _popcorn.muted();
b1c3bf6eca78 Cleaning and refactoring
veltr
parents: 967
diff changeset
   109
        _media.volume = _popcorn.volume();
b1c3bf6eca78 Cleaning and refactoring
veltr
parents: 967
diff changeset
   110
    }
1068
7623f9af9272 merge pull request #3 from O. Auber
ymh <ymh.work@gmail.com>
parents: 1044
diff changeset
   111
970
b1c3bf6eca78 Cleaning and refactoring
veltr
parents: 967
diff changeset
   112
    _popcorn.on("loadedmetadata", function() {
b1c3bf6eca78 Cleaning and refactoring
veltr
parents: 967
diff changeset
   113
        getVolume();
b1c3bf6eca78 Cleaning and refactoring
veltr
parents: 967
diff changeset
   114
        _media.trigger("loadedmetadata");
b1c3bf6eca78 Cleaning and refactoring
veltr
parents: 967
diff changeset
   115
        _media.trigger("volumechange");
1013
392ddcd212d7 Throwed in a bunch of semicolons
veltr
parents: 987
diff changeset
   116
    });
1068
7623f9af9272 merge pull request #3 from O. Auber
ymh <ymh.work@gmail.com>
parents: 1044
diff changeset
   117
958
2aa7fdb0762a Commit before branch switch (for tests)
veltr
parents:
diff changeset
   118
    _popcorn.on("timeupdate", function() {
970
b1c3bf6eca78 Cleaning and refactoring
veltr
parents: 967
diff changeset
   119
        _media.trigger("timeupdate", new IriSP.Model.Time(1000*_popcorn.currentTime()));
958
2aa7fdb0762a Commit before branch switch (for tests)
veltr
parents:
diff changeset
   120
    });
1068
7623f9af9272 merge pull request #3 from O. Auber
ymh <ymh.work@gmail.com>
parents: 1044
diff changeset
   121
970
b1c3bf6eca78 Cleaning and refactoring
veltr
parents: 967
diff changeset
   122
    _popcorn.on("volumechange", function() {
b1c3bf6eca78 Cleaning and refactoring
veltr
parents: 967
diff changeset
   123
        getVolume();
b1c3bf6eca78 Cleaning and refactoring
veltr
parents: 967
diff changeset
   124
        _media.trigger("volumechange");
1013
392ddcd212d7 Throwed in a bunch of semicolons
veltr
parents: 987
diff changeset
   125
    });
1068
7623f9af9272 merge pull request #3 from O. Auber
ymh <ymh.work@gmail.com>
parents: 1044
diff changeset
   126
1069
2409cb4cebaf getting various changes from github
ymh <ymh.work@gmail.com>
parents: 1068
diff changeset
   127
    _popcorn.on("play", function(e) {
970
b1c3bf6eca78 Cleaning and refactoring
veltr
parents: 967
diff changeset
   128
        _media.trigger("play");
b1c3bf6eca78 Cleaning and refactoring
veltr
parents: 967
diff changeset
   129
    });
1068
7623f9af9272 merge pull request #3 from O. Auber
ymh <ymh.work@gmail.com>
parents: 1044
diff changeset
   130
970
b1c3bf6eca78 Cleaning and refactoring
veltr
parents: 967
diff changeset
   131
    _popcorn.on("pause", function() {
b1c3bf6eca78 Cleaning and refactoring
veltr
parents: 967
diff changeset
   132
        _media.trigger("pause");
b1c3bf6eca78 Cleaning and refactoring
veltr
parents: 967
diff changeset
   133
    });
1068
7623f9af9272 merge pull request #3 from O. Auber
ymh <ymh.work@gmail.com>
parents: 1044
diff changeset
   134
970
b1c3bf6eca78 Cleaning and refactoring
veltr
parents: 967
diff changeset
   135
    _popcorn.on("seeked", function() {
b1c3bf6eca78 Cleaning and refactoring
veltr
parents: 967
diff changeset
   136
        _media.trigger("seeked");
b1c3bf6eca78 Cleaning and refactoring
veltr
parents: 967
diff changeset
   137
    });
1068
7623f9af9272 merge pull request #3 from O. Auber
ymh <ymh.work@gmail.com>
parents: 1044
diff changeset
   138
7623f9af9272 merge pull request #3 from O. Auber
ymh <ymh.work@gmail.com>
parents: 1044
diff changeset
   139
};