function flashPlayerTimeListener(obj) { var player = document.getElementById(obj.id); player.position = obj.position; } function flashPlayerStateListener(obj) { var player = document.getElementById(obj.id); player.state = obj.newstate; } YUI.add('video-player', function(Y) { var Lang = Y.Lang, Widget = Y.Widget, Node = Y.Node; var NS = Y.namespace('mazzle'); NS.VideoPlayer = VideoPlayer; /* VideoPlayer class constructor */ function VideoPlayer(config) { VideoPlayer.superclass.constructor.apply(this, arguments); } /* * Required NAME static field, to identify the Widget class and * used as an event prefix, to generate class names etc. (set to the * class name in camel case). */ VideoPlayer.NAME = "videoplayer"; /* * The attribute configuration for the VideoPlayer widget. Attributes can be * defined with default values, get/set functions and validator functions * as with any other class extending Base. */ VideoPlayer.ATTRS = { src: { value: "" }, height: { value: 200 }, width: { value: 300 }, state: { // "BUFFERING", "PAUSED", "PLAYING", "FINISHED" value: "STOPPED" }, duration: { // in ms value: null }, autoplay: { value: false }, start: { value: 0 }, controls: { value: true }, playerType : { value: "auto" }, extensions : { value: { mp4:"flash", flv:"flash", asf:"silverlight", wmv:"silverlight" } }, filepath : { value: '/js/videoplayer/' } }; /* VideoPlayer extends the base Widget class */ Y.extend(VideoPlayer, Widget, { initializer: function() { if(this.get("playerType")=="auto") { this._guessPlayerType(); } }, destructor : function() { }, renderUI : function() { this._renderContent(); }, bindUI : function() { }, syncUI : function() { }, _renderContent : function() { var playerType = this.get("playerType"); if(playerType=="html") { this._renderHTMLPlayer(); } else if(playerType=="flash") { this._renderFlashPlayer(); } else if(playerType=="silverlight") { this._renderSilverlightPlayer(); } else if(playerType=="npo") { this._renderNPOPlayer(); } }, /* HTML5 */ _renderHTMLPlayer : function() { var content = this.get("contentBox"), src = this.get("src"), height = this.get("height"), width = this.get("width"), id = Y.guid(); var video = '')); var cfg = { file:src, width:width, height:height, //linkfromdisplay:'true', autostart:'true' }; if(!this.get("controls")) {cfg.shownavigation='false'} this.player = new jeroenwijering.Player(video._node,xaml,cfg); this._addSilverlightListeners(); this._silverlightJumpStart(); }, _addSilverlightListeners : function() { if(this.player.view) { var oSelf = this, player = this.player; player.addListener('TIME', function(time) { oSelf._changeCurrentTime(time); }); player.addListener('STATE', function(oldState, newState) { oSelf._changeState(newState); }); } else { Y.later(100, this, this._addSilverlightListeners); } }, /* Flash */ _renderFlashPlayer : function() { var url = this.get('filepath')+'swfobject.js'; if(typeof swfobject == "undefined") { Y.Get.script(url, { onSuccess: this._embedFlashPlayer, context: this }); } else { this._embedFlashPlayer(); } }, _embedFlashPlayer : function() { Y.log('create flash player'); var content = this.get("contentBox"), height = this.get("height"), width = this.get("width"), id = Y.guid(), playerId = id+"_player", swf = this.get('filepath')+'player.swf'; var flashvars = { file: this.get("src"), autostart: 'true' }; if(!this.get("controls")) {flashvars.controlbar='none'} var attributes = { 'id': playerId, 'name': playerId }; var params = { 'allowscriptaccess': 'always' }; content.appendChild(Node.create('
')); swfobject.embedSWF(swf,id,width,height,'9','false',flashvars,params,attributes); this.playerId = playerId; this._flashJumpStart(); this._addFlashListeners(); }, _addFlashListeners : function() { try { var player = document.getElementById(this.playerId); // add listeners player.addModelListener("Time", "flashPlayerTimeListener"); player.addModelListener("State", "flashPlayerStateListener"); this.player = player; this._flashTimeCheck(); this._flashStateCheck(); } catch(e) { Y.later(100, this, this._addFlashListeners); } }, /* NPO silverlight */ _renderNPOPlayer : function() { // make sure we have the required scripts loaded var filepath = this.get('filepath'), urls = [ filepath+'silverlight.js', filepath+'ugslplayer.js' ]; Y.Get.script(urls, { onSuccess: this._embedNPOPlayer, context: this }); }, _embedNPOPlayer : function() { var src = this.get("src"); Y.log('create npo player'); var content = this.get("contentBox"), height = this.get("height"), width = this.get("width"), start = this.get("start"), controls = this.get("controls") ? 'yes' : 'no', id = Y.guid(); var cfg = { width : width, height : height, volume : '100', showBorder : 'no', embedEnabled : 'yes', controlBarEnabled : controls, viewMode : 'video', //playMode : autoplay ? 'play' : 'pause', playlistEnabled : 'no', playlistAdvanceEnabled : 'no', fragmentID : src //callbackFunction : }; var video = content.append(Node.create('
')); this.player = new UGSLPlayer(document.getElementById(id), 'http://embed.player.omroep.nl/slg/ugslplayer.xap', cfg); }, _ugCallbackFunction : function(eventType, args) { if (eventType = 'PropertyChanged') { console.log('property %s changed from %s to %s', args.name, args.oldvalue, args.newvalue); if(args.name=='seekTime') { this._changeCurrentTime(arg.newvalue); } else if(args.name=='videostatus') { } /* if (args.name == 'seekTime' || args.name == 'videoStatus') { var streamStatus = vp.player.getConfigProperty('videoStatus'); if (streamStatus != 'playing') { return; } var seekTime = Utils.parseTime(vp.player .getConfigProperty('seekTime')); var startTime = Utils.parseTime(vp.player .getConfigProperty('startTime')); var endTime = Utils.parseTime(vp.player .getConfigProperty('endTime')); if (!startTime || !endTime) { return; } var duration = endTime - startTime; var elapsed = seekTime; if (elapsed > duration - 5) { vp.nearEnd = true; } vp.onTick(elapsed, duration); } else if (args.name == 'playMode' && args.newvalue == 'pause' && vp.nearEnd) { vp.dispatchEvents("fragmentEnd"); }*/ } }, /* methods */ getDuration : function() { var pt = this.get("playerType"), p = this.player; if(this.duration) { return this.duration; } else { if(pt=="silverlight") { return p.configuration.duration; } else if(pt=="flash") { return p.getConfig().duration; } } }, // returns current playHead in ms getTime : function() { var p = this.player; if(p.currentTime) { return p.currentTime; } else { return 0; } }, // sets current playHead in ms setTime : function(time, play) { Y.log('seek to '+time); var pt = this.get("playerType"), p = this.player, oldtime = this.getTime(); if(pt=="silverlight") { p.sendEvent('SCRUB', time); if(play) { this.play(); } } else if(pt=="npo") { var h = parseInt(time/3600) % 24, m = parseInt(time/60) % 60, s = time % 60, hms = (h<10 ? "0"+h : h) + ":" + (m<10 ? "0"+m : m) + ":" + (s<10 ? "0"+s : s); console.log(hms); p.content.ugslAccess.setConfigProperty('seekTime', hms); } else if(pt=="flash"){ p.sendEvent('SEEK', time); if(!play) { Y.later(100, this, this.pause); } } else { p.currentTime = time; if(play) {this.play();} } this.fire("timeSet", {oldtime:oldtime, newtime:time}); }, loadVideo : function(src) { Y.log('load '+src); var oldPt = this.get("playerType"), p = this.player; this.set("src", src); var pt = this._guessPlayerType(); if(pt==oldPt) { if(pt=="silverlight"||pt=="flash") { // TBD something fails here p.sendEvent("LOAD", src); } } else { this._renderContent(); } this.fire("videoLoad", {src:src, playerType:pt}); }, play : function() { var pt = this.get("playerType"), p = this.player, state = this.get("state"); if(!(state=='BUFFERING'||state=='PLAYING')) { if(pt=="html") { p.play(); this._changeState("PLAYING"); } else { p.sendEvent('PLAY'); } } }, pause : function() { var pt = this.get("playerType"), p = this.player, state = this.get("state"); if(state=='PLAYING'||state=='BUFFERING') { if(pt=="html") { p.pause(); this._changeState("PAUSED"); } else { p.sendEvent('PLAY'); } } }, stop : function() { var pt = this.get("playerType"), p = this.player; if(pt=="html") { p.stop(); this._changeState("STOPPED"); } else { p.sendEvent('STOP'); } }, _guessPlayerType : function() { // guess required playerType based on extension if(this.get("src")) { var pt = null, src = this.get("src"), extensions = this.get("extensions"), ext = src.substr(src.length-3, 3); if(parseInt(src)) { pt = 'npo'; } else if(extensions[ext]) { pt = extensions[ext]; } else { pt = "html"; } } this.set("playerType", pt); return pt; }, _silverlightJumpStart : function() { var p = this.player, state = this.get("state"); // we made the silverlight player autoplay by default // (to load the file) // and now we pause it when we did not want that. if(p.controller&&state=='PLAYING') { this.fire("bufferReady"); var start = this.get("start"); if(!this.get("autoplay")) { p.sendEvent('PLAY'); } if(start) { p.sendEvent('SCRUB', start); } } else { Y.later(100, this, this._silverlightJumpStart); } }, _flashJumpStart : function() { var p = this.player, state = this.get("state"), start = this.get("start"); // we made the player autoplay by default // (to load the file) // and now we pause it when we did not want that. if(p) { this.fire("bufferReady"); var start = this.get("start"); if(!this.get("autoplay")) { p.sendEvent('PLAY'); } if(start) { p.sendEvent('SEEK', start); } } else { Y.later(100, this, this._flashJumpStart); } }, _flashStateCheck : function() { this._changeState(this.player.state); Y.later(100, this, this._flashStateCheck); }, _changeState : function(newState) { newState = newState.toUpperCase(); var oldState = this.get("state"); if(newState&&oldState!==newState) { this.set("state", newState); Y.log('state: '+newState); this.fire("stateChanged", {oldstate:oldState, newstate:newState}); if(newState=="COMPLETED") { this.fire("end"); } } }, _flashTimeCheck : function() { this._changeCurrentTime(this.player.position); Y.later(100, this, this._flashTimeCheck); }, _changeCurrentTime : function(time) { var player = this.player; if(player.currentTime!==time) { player.currentTime = time; this.fire("timeChange", {time:time}); } } }); }, 'gallery-2010.03.02-18' ,{requires:['node','event','widget']});