github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/public/libs/video-js/videojs-playlists.js (about) 1 /*! 2 * videojs-playlists - Playlists done right for Videojs 3 * v0.1.1 4 * 5 * copyright Antonio Laguna 2013 6 * MIT License 7 */ 8 //videojs-playlists.js 9 function playList(options,arg){ 10 var player = this; 11 player.pl = player.pl || {}; 12 var index = parseInt(options,10); 13 14 player.pl._guessVideoType = function(video){ 15 var videoTypes = { 16 'webm' : 'video/webm', 17 'mp4' : 'video/mp4', 18 'ogv' : 'video/ogg' 19 }; 20 var extension = video.split('.').pop(); 21 22 return videoTypes[extension] || ''; 23 }; 24 25 player.pl.init = function(videos, options) { 26 options = options || {}; 27 player.pl.videos = []; 28 player.pl.current = 0; 29 player.on('ended', player.pl._videoEnd); 30 31 if (options.getVideoSource) { 32 player.pl.getVideoSource = options.getVideoSource; 33 } 34 35 player.pl._addVideos(videos); 36 }; 37 38 player.pl._updatePoster = function(posterURL) { 39 player.poster(posterURL); 40 player.removeChild(player.posterImage); 41 player.posterImage = player.addChild("posterImage"); 42 }; 43 44 player.pl._addVideos = function(videos){ 45 for (var i = 0, length = videos.length; i < length; i++){ 46 var aux = []; 47 for (var j = 0, len = videos[i].src.length; j < len; j++){ 48 aux.push({ 49 type : player.pl._guessVideoType(videos[i].src[j]), 50 src : videos[i].src[j] 51 }); 52 } 53 videos[i].src = aux; 54 player.pl.videos.push(videos[i]); 55 } 56 }; 57 58 player.pl._nextPrev = function(func){ 59 var comparison, addendum; 60 61 if (func === 'next'){ 62 comparison = player.pl.videos.length -1; 63 addendum = 1; 64 } 65 else { 66 comparison = 0; 67 addendum = -1; 68 } 69 70 if (player.pl.current !== comparison){ 71 var newIndex = player.pl.current + addendum; 72 player.pl._setVideo(newIndex); 73 player.trigger(func, [player.pl.videos[newIndex]]); 74 } 75 }; 76 77 player.pl._setVideo = function(index){ 78 if (index < player.pl.videos.length){ 79 player.pl.current = index; 80 player.pl.currentVideo = player.pl.videos[index]; 81 82 if (!player.paused()){ 83 player.pl._resumeVideo(); 84 } 85 86 if (player.pl.getVideoSource) { 87 player.pl.getVideoSource(player.pl.videos[index], function(src, poster) { 88 player.pl._setVideoSource(src, poster); 89 }); 90 } else { 91 player.pl._setVideoSource(player.pl.videos[index].src, player.pl.videos[index].poster); 92 } 93 } 94 }; 95 96 player.pl._setVideoSource = function(src, poster) { 97 player.src(src); 98 player.pl._updatePoster(poster); 99 }; 100 101 player.pl._resumeVideo = function(){ 102 player.one('loadstart',function(){ 103 player.play(); 104 }); 105 }; 106 107 player.pl._videoEnd = function(){ 108 if (player.pl.current === player.pl.videos.length -1){ 109 player.trigger('lastVideoEnded'); 110 } 111 else { 112 player.pl._resumeVideo(); 113 player.next(); 114 } 115 }; 116 117 if (options instanceof Array){ 118 player.pl.init(options, arg); 119 player.pl._setVideo(0); 120 return player; 121 } 122 else if (index === index){ // NaN 123 player.pl._setVideo(index); 124 return player; 125 } 126 else if (typeof options === 'string' && typeof player.pl[options] !== 'undefined'){ 127 player.pl[options].apply(player); 128 return player; 129 } 130 } 131 132 videojs.Player.prototype.next = function(){ 133 this.pl._nextPrev('next'); 134 return this; 135 }; 136 videojs.Player.prototype.prev = function(){ 137 this.pl._nextPrev('prev'); 138 return this; 139 }; 140 141 videojs.plugin('playList', playList);