var later = function(when, o, fn, data) { when = when || 0; o = o || {}; var f = setTimeout(function() {fn.apply(o, data)}, when); return { id: f, cancel: function() { clearTimeout(r); } }; }; function flashPlayerTimeListener(obj) { document.getElementById(obj.id).position = obj.position; } function flashPlayerStateListener(obj) { document.getElementById(obj.id).state = obj.newstate; } (function($) { $.widget("ui.videoplayer", { state: 'init', options: { src: "", height: 200, width: 300, duration: null, autostart: false, start: 0, playerType: null, extensions: { flv:"flash", asf:"silverlight", wmv:"silverlight" }, filepath: '/js/videoplayer/' }, _create: function() { var o = this.options; if(!o.playerType) { this._setPlayerType(); } this.element .addClass("ui-videoplayer ui-widget ui-corner-all"); this._renderContent(); }, _setPlayerType : function() { // guess required playerType based on extension var o = this.options; if(o.src) { var pt, src = o.src, extensions = o.extensions, videoType = src.substr(src.length-3, 3); if(extensions[videoType]) { pt = extensions[videoType]; } else { pt = "html"; } } o.playerType = pt; return pt; }, _renderContent : function() { switch(this.options.playerType) { case "flash": this._renderFlashPlayer() break; case "silverlight": this._renderSilverlightPlayer() break; default: this._renderHTMLPlayer() } }, _renderHTMLPlayer : function() { var o = this.options; this.player = $('') .attr({ src: o.src, height: o.height, width: o.width }) .appendTo(this.element); }, _renderFlashPlayer : function() { //$.log('create flash player'); var o = this.options, id = this.element[0].id+'1', playerId = id+"_player", swf = o.filepath+'player.swf', width = o.width, height = o.height; var flashvars = { file: o.src, 'autostart': 'true' }; var attributes = { 'id': playerId, 'name': playerId }; var params = { 'allowscriptaccess': 'always' }; this.element.append('
'); swfobject.embedSWF(swf,id,width,height,'9','false',flashvars,params,attributes); this._flashPlayerInit(playerId); }, _flashPlayerInit : function(playerId) { try { var p = document.getElementById(playerId), o = this.options; // as we can't refer directly to the object we first store the data in the player object // our own listeners than reads out this data. // Do you have a more elegant solution? p.addModelListener("Time", "flashPlayerTimeListener"); p.addModelListener("State", "flashPlayerStateListener"); this.player = p; this._flashTimeCheck(); this._flashStateCheck(); // we made the player autostart by default to load the file // now we first seek to the start time and pause if we didn't // want it to autoplay if(o.start) { p.sendEvent('SEEK', o.start); } if(!o.autostart) { p.sendEvent('PLAY'); } } catch(e) { later(100, this, this._flashPlayerInit, [playerId]); } }, getDuration : function() { var p = this.player; switch(this.options.playerType) { case "flash": return p.getConfig().duration break; case "silverlight": return p.configuration.duration break; default: return p.duration // is this correct? } }, getTime : function() { var p = this.player; if(p.currentTime) { return p.currentTime; } else { return 0; } }, setTime : function(time, play) { //$.log('seek to '+time); var p = this.player; //oldtime = this.getTime(); switch(this.options.playerType) { case "flash": p.sendEvent('SEEK', time); if(!play) { later(100, this, this.pause); } break; case "silverlight": p.sendEvent('SCRUB', time); if(play) { this.play() }; break; default: p.currentTime(time); if(play) { this.play() }; } //this.fire("timeSet", {oldtime:oldtime, newtime:time}); }, play : function() { var p = this.player, state = this.state; if(!(state=='BUFFERING'||state=='PLAYING')) { if(this.options.playerType=="html") { p.play(); this._changeState("PLAYING"); } else { p.sendEvent('PLAY'); } } }, pause : function() { var p = this.player, state = this.state; if(state=='PLAYING'||state=='BUFFERING') { if(this.options.playerType=="html") { p.pause(); this._changeState("PAUSED"); } else { p.sendEvent('PLAY'); } } }, stop : function() { var p = this.player; if(this.options.playerType=="html") { p.stop(); this._changeState("STOPPED"); } else { p.sendEvent('STOP'); } }, _flashStateCheck : function() { this._changeState(this.player.state); later(100, this, this._flashStateCheck); }, _changeState : function(newState) { if(newState) { newState = newState.toUpperCase(); var oldState = this.state; if(oldState!==newState) { this.state = newState; //this.fire("stateChanged", {oldstate:oldState, newstate:newState}); } } }, _flashTimeCheck : function() { this._changeCurrentTime(this.player.position); later(100, this, this._flashTimeCheck); }, _changeCurrentTime : function(time) { if(time) { var player = this.player; if(player.currentTime!==time) { player.currentTime = time; //this.fire("timeChange", {time:time}); } } }, destroy : function() { } }); $.extend($.ui.videoplayer, {}); })(jQuery);