/** * Use one MediaTracker object for each video/audio player on your page. * @markdown // get tracker object and set some properties in constructor var tracker = new econda.media.MediaTracker({ content: { name: "Wetten dass... - Folge 9142", category: "/Unterhaltung/Wetten dass..." }, type: "video" }); // set one more property tracker.setDuration(150); // video length in seconds // initialize it tracker.init(); // we would do this not here... // start playback tracker.setState("playing", 0); // setState accepts the current position // we could write this as... // tracker.setPosition(0).setState("playing"); // but one line is more handy // send user rewind tracker.setState("paused", 30); tracker.setState("playing", 5); // user clicked "stop video" button tracker.setStated("stopped", 60); * * @class econda.media.MediaTracker * @extends econda.base.Object * @param cnf * @returns {econda.media.MediaTracker} */ econda.media.MediaTracker = function(cfg) { var cfg = cfg || {}; /** * Private properties collection * @property {Object} __p * @private */ var __p = { /** * @cfg {econda.media.Content} content * Content object */ content: null, /** * @cfg {String} [type="video"] * Media type, "audio" or "video" */ type: "video", /** * @cfg {Number} duration * Duration in seconds */ duration: null, /** * @cfg {String} previewUri * URI of preview video (absulte uri, including protocal, host...). Preview media files * must be accessible via SSL (https://). */ previewUri: null, /** * @cfg {Number} position * Current playback position */ position: 0, /** * @cfg {String} state * Current playback state uninitialized|initialized|playing|paused|stopped */ state: 'uninitialized' // uninitialized|initialized|playing|paused|stopped }; /** * @property {Number} __instanceId Id of this tracker instance * @private */ this.__instanceId = econda.media.MediaTracker.instanceCount++; /** * @method * Initialize tracker. Properties must be set at this point */ this.init = function() { // check if we've all required data var ok = true; ok = ok && (__p["content"] instanceof econda.media.Content && "" != __p["content"].getName()); ok = ok && (__p["type"] == "audo" || __p["type"] == "video"); ok = ok && (__p["duration"] != null); if(ok) { econda.debug.log("MediaTracker initialized.", this); __p["state"] = "initialized"; } else { econda.debug.error("Could not initialize MediaTracker.", this); } return this; }; /** * Set content information * @markdown Example: tracker.setContent({ name: "MyVideo", categories: ["/Produktvideos", "/ImageVideos"] }); * @param {econda.media.Content/Object/String} */ this.setContent = function(content) { if( this.getState() == 'playing' || this.getState() == 'paused') { econda.debug.error("Trying to modify content information on an already playing tracker."); } else { __p["content"] = new econda.media.Content(content); } return this; }; /** * Get current content information * * @method * @return {econda.media.Content} */ this.getContent = function() { return __p["content"]; }; /** * Set duration of media * @method * @param {Number} seconds Duration in seconds * @returns {econda.media.MediaTracker} */ this.setDuration = function(seconds) { __p["duration"] = Number(seconds); return this }; /** * Get defined duration of current media * * @method * @returns {Number} duration in seconds */ this.getDuration = function() { return __p["duration"]; }; /** * Set current playback position * @methid * @param {Number} seconds Current position in seconds */ this.setPosition = function(seconds) { if(typeof seconds != 'number') { econda.debug.error("Got invalid value for position: " + seconds); } else { __p["position"] = seconds; } return this; }; /** * Get current playback position as stored in tracker. * This might not be the current position in your media player! * * @method * @return {Number} seconds since start */ this.getPosition = function() { return __p["position"]; }; /** * Set preview uri * @method * @param {String} uri Uri of preview media file. Must be an SSL URI (https://...) */ this.setPreviewUri = function(uri) { if(typeof uri != 'string' && uri != null) { econda.debug.error("Got invalid value for MediaTracker.previewUri: " + uri); } else { if(typeof uri == 'string' && uri.indexOf("https://") != 0) { econda.debug.error("preview uri MUST be a valid SSL uri!"); } else { __p["previewUri"] = uri; } } return this; }; /** * Get preview uri * @return {String} */ this.getPreviewUri = function() { return __p["previewUri"]; }; /** * Set current state and optional current position * @method * @param {String} state playing|paused|stopped * @param {Number} position current plaback position in seconds * @return {econda.media.MediaTracker} */ this.setState = function(state, position) { var oldState; // check and set new state if(typeof state != 'string') { econda.debug.error("State value must be a string."); } else { oldState = __p["state"]; state = state.toLowerCase(); // check for valid state values if(econda.util.Array.indexOf(['playing', 'paused', 'stopped'], state) == -1) { econda.debug.error("Invalid state value given: " + state); } else { // tracker should be initialized first if(__p["state"] == "uninitialized" && state != "initialized") { econda.debug.warn("Got playing/paused/stopped state changed but MediaTracker is uninitialized. Please initialize tracker first.", this); } __p["state"] = state; // check and set position if(typeof position != 'undefined') { __p["position"] = Number(position); } if(oldState != state) { // Send event via proxy var eventNames = { playing: "play", paused: "pause", stopped: "stop" }; this.sendMediaEvent(eventNames[state]); } } } return this; }; /** * Helper to send media events using configured proxy * @method * @private */ this.sendMediaEvent = function(eventName) { var cmp = this, event = new econda.proxy.MediaEvent({ contentLabel: cmp.getContent().toString(), eventName: eventName, mediaType: cmp.getType(), position: Math.round(cmp.getPosition()), duration: Math.round(cmp.getDuration()), previewUri: cmp.getPreviewUri(), trackerId: cmp.__instanceId }); this.getProxy().sendMediaEvent(event); }; /** * Get current playback state * @method * @return {String} */ this.getState = function() { return __p["state"]; }; /** * Get used proxy instance * @method * @return {econda.proxy.Direct} */ this.getProxy = function() { return econda.proxy.getInstance(); }; /** * Set content type * @param {String} type Must be "audio" or "video" */ this.setType = function(type) { if(typeof type != 'string') { econda.debug.error("MediaTracker.setType expects as String. Got: " + type, type); } else { type = type.toLowerCase(); if(type != 'audio' && type != 'video') { econda.debug.error("MediaTracker.setType expects 'audio' or 'video' as parameter. Got: " + type, type); } else { __p["type"] = type; } } return this; }; /** * Returns content type * @returns {String} media type (video/audio) */ this.getType = function() { return __p["type"]; }; // Constructor return this.setup(cfg, __p); }; econda.media.MediaTracker.prototype = new econda.base.Object(); /** * @property {Number} instanceCount Number of instances created * @private */ econda.media.MediaTracker.instanceCount = 0;