/* * MediaHelper */ var MediaHelper = { /* NICK VIDEO */ MTVN_VIDEO_WIDTH: 574, MTVN_VIDEO_HEIGHT: 320, setMTVNSize: function() { if ($(".video_container.question_video").length == 0) { return; } var vidRatio = this.MTVN_VIDEO_HEIGHT / this.MTVN_VIDEO_WIDTH; var vidDynamicW = $(".video_container.question_video").width(); var vidDynamicH = Math.round(vidDynamicW * vidRatio); $(".video_container.question_video, .video_container.question_video object, .video_container.question_video iframe").height(vidDynamicH); }, waitForMTVN: function() { if ($(".video_container.question_video").length == 1) { this.setMTVNSize(); } else { setTimeout(this.waitForMTVN.ctx(this), 100); } }, /* NICK VIDEO */ webaudio: false, device: "desktop", static_url: "", media_players: {}, _media_player_available: function() { var nav = navigator; var u = nav.userAgent.toLowerCase(); var mac = /macintosh/.test(u); var ff = /firefox/.test(u); var flash = ((typeof nav.plugins != "undefined" && typeof nav.plugins["Shockwave Flash"] == "object") && (nav.plugins["Shockwave Flash"].description && !(typeof nav.mimeTypes != "undefined" && nav.mimeTypes["application/x-shockwave-flash"] && !nav.mimeTypes["application/x-shockwave-flash"].enabledPlugin))); if (mac && ff && !flash) { return false; } return true; }, _on_media_playing: function(id) { var d = MediaHelper.media_players[id]; if (!d) { return; } if (d.type == "video") { $(".video_play_overlay", d.container).hide(); } if (d.type == "audio") { $(".audio_play_overlay", d.container).hide(); } if (d.on_start) { d.on_start(); } if (d.timeout != null) { return; } if (d.type == "audio" && MediaHelper.webaudio) { // no need, we can reliably detect this anyway } else { if (!d.loop) { var last_time = 0; d.timeout = setInterval(function() { if (last_time > 0 && last_time > d.element.currentTime) { MediaHelper._on_media_stopping(id); } last_time = d.element.currentTime; }, 500); } } }, _on_media_stopping: function(id) { var d = MediaHelper.media_players[id]; if (!d) { return; } if (d.type == "audio" && MediaHelper.webaudio && d.loop) { WebAudioSystem.playSound(id, function() { MediaHelper._on_media_stopping(id); }); } if (d.type == "video" && d.replay) { $(".video_play_overlay", d.container).fadeIn(); } if (d.type == "audio" && d.replay) { $(".audio_play_overlay", d.container).fadeIn(); } if (d.timeout != null) { clearInterval(d.timeout); d.timeout = null; } if (d.on_finish) { d.on_finish(); } }, init: function(params) { MediaHelper.static_url = params.static_url || ""; MediaHelper.device = params.device || "desktop"; MediaHelper.webaudio = WebAudioSystem.available(); if (MediaHelper.webaudio) { WebAudioSystem.init(); } }, initialise_media: function(type, container, url, options) { container = $(container); options = options || {}; var id = (options.id || "media_" + (new Date()).getTime()); var autoplay = (options.autoplay || false); var muted = (options.muted || false); var controls = !(options.controls === false); var on_init = (options.on_init || null); var on_start = (options.on_start || null); var on_finish = (options.on_finish || null); var replay = (options.replay || false); var loop = (options.loop || false); var mime_type = (options.mime_type || ""); if (MediaHelper.device != "desktop") { autoplay = false; } var mtvn = (options.mtvn || false); if(mtvn){ type = "mtvn"; } switch (type) { case "mtvn": container.html('
'); if (typeof MTVNPlayer !== 'undefined') { var config = {width: "100%", height: "100%", uri: url, flashVars: { autoPlay: (autoplay ? "true" : "false"), sid: "12345" }}; var on_play = this._on_media_playing.ctx(this, id); var on_stop = this._on_media_stopping.ctx(this, id); var events = { onStateChange: function(event) { if (event.data == "playing") { on_play(); } if (event.data == "" || event.data == "stopped") { on_stop(); } } }; var mtvn_player = MTVNPlayer.Player(id, config, events); this.media_players[id] = { loop: false, mtvn: true, replay: replay, type: type, container: container, element: mtvn_player, on_start: on_start, on_finish: on_finish, timeout: null }; this.waitForMTVN(); } else { alert("MTVNPlayer not loaded properly. Please refresh your page and try again."); } break; case "video" : if (mime_type == "") { mime_type = "video/mp4"; } container.html('
' + (controls ? ('
') : '') + '
'); if (controls) { $(".video_play_overlay", container).off("click").on("click", function(e) { e.preventDefault(); MediaHelper.play_media(id); }); if (autoplay) { $(".video_play_overlay", container).hide(); } } break; case "audio" : if (mime_type == "") { mime_type = "audio/mpeg"; } if (MediaHelper.webaudio) { container.html('
' + (controls ? ('
') : '') + '
'); } else { container.html('
' + (controls ? ('
') : '') + '
'); } if (controls) { $(".audio_play_overlay", container).off("click").on("click", function(e) { e.preventDefault(); MediaHelper.play_media(id); }); if (autoplay) { $(".audio_play_overlay", container).hide(); } } break; } if (type == "mtvn" ) { } else if (type == "audio" && MediaHelper.webaudio) { WebAudioSystem.loadSound(id, url, function() { if (console && console.log) { console.log("WebAudioSystem initialised for " + url); } if (on_init) { on_init(); } if (autoplay) { MediaElement.play_media(id); } }); MediaHelper.media_players[id] = {loop: loop, replay: replay, type: type, container: container, element: null, on_start: on_start, on_finish: on_finish, timeout: null}; } else { var me = MediaElement(id, { pluginPath: MediaHelper.static_url + 'apps/swf/', customError: ' ', success: function(mediaElement) { if (console && console.log) { console.log("MediaElement initialised " + mediaElement.pluginType + " for " + url); } if (on_init) { on_init(); } if (autoplay && mediaElement.pluginType == 'flash') { mediaElement.addEventListener('canplay', function() { mediaElement.play(); }, false); } mediaElement.addEventListener('playing', function() { MediaHelper._on_media_playing(id); }); if (!loop) { mediaElement.addEventListener('ended', function() { MediaHelper._on_media_stopping(id); }); } } }); MediaHelper.media_players[id] = {loop: loop, replay: replay, type: type, container: container, element: me, on_start: on_start, on_finish: on_finish, timeout: null}; } }, play_media: function(id) { if (!MediaHelper._media_player_available()) { alert("Media player unavailable"); return; } var d = MediaHelper.media_players[id]; if (!d) { return; } if (d.type == "audio" && MediaHelper.webaudio) { WebAudioSystem.playSound(id, function() { MediaHelper._on_media_stopping(id); }); MediaHelper._on_media_playing(id); } else if(d.type == "mtvn"){ }else{ try { d.element.pause(); d.element.setCurrentTime(0); } catch (er) { } if (d.element.currentTime > 0) { d.element.load(); } d.element.play(); } }, kill_all_media: function(container) { $("video, audio", $(container)).each(function(ix, media) { if ($(media).attr("id")) { MediaHelper.kill_media($(media).attr("id")); } }); }, kill_media: function(id) { var d = MediaHelper.media_players[id]; if (!d) { return; } if (d.type == "audio" && MediaHelper.webaudio) { WebAudioSystem.stopSound(id); } else { try { d.element.removeEventListener('playing'); d.element.removeEventListener('ended'); } catch (e) { } try { d.element.remove(); } catch (e) { } } MediaHelper._on_media_stopping(id); MediaHelper.media_players[id] = null; } }; /** * Web Audio API */ var WebAudioSystem = { isUnlocked: false, iOS: false, buffers: {}, cache: {}, sources: {}, available: function() { return !!(window.AudioContext || window.webkitAudioContext) && !!(XMLHttpRequest); }, _newContext: function() { return new (window.AudioContext || window.webkitAudioContext)(); }, init: function() { WebAudioSystem.iOS = (window.navigator.userAgent.match(/ipad/i) !== null) || (window.navigator.userAgent.match(/ipod/i) !== null) || (window.navigator.userAgent.match(/iphone/i) !== null); WebAudioSystem.context = WebAudioSystem._newContext(); WebAudioSystem.isUnlocked = false; }, _unlock: function() { if (WebAudioSystem.isUnlocked) { return; } var source = WebAudioSystem.context.createBufferSource(); source.buffer = WebAudioSystem.context.createBuffer(1, 1, 22050); source.connect(WebAudioSystem.context.destination); if (source.start) { source.start(0); } else { source.noteOn(0); } // by checking the play state after some time, we know if we're really unlocked setTimeout(function() { if ((source.playbackState === source.PLAYING_STATE || source.playbackState === source.FINISHED_STATE)) { WebAudioSystem.isUnlocked = true; } }, 0); }, loadSound: function(id, url, on_success) { var request = new XMLHttpRequest(); request.responseType = "arraybuffer"; request.onload = function() { WebAudioSystem.cache[id] = request.response; WebAudioSystem.context.decodeAudioData(request.response, function(buffer) { WebAudioSystem.buffers[id] = { sampleRate: WebAudioSystem.context.sampleRate, buffer: buffer }; if (on_success) { on_success(); } }); }; request.open('GET', url, true); request.send(); }, playSound: function(id, on_finish) { var buffer = WebAudioSystem.buffers[id]; if (!buffer) { return; } if (WebAudioSystem.iOS) { // check the context has changed sample rate var newContext = WebAudioSystem._newContext(); if (WebAudioSystem.context.sampleRate != newContext.sampleRate) { WebAudioSystem.context = newContext; WebAudioSystem.isUnlocked = false; } WebAudioSystem._unlock(); // do we need to re-decode this sound? if (buffer.sampleRate != WebAudioSystem.context.sampleRate) { WebAudioSystem.context.decodeAudioData(WebAudioSystem.cache[id], function(buffer) { WebAudioSystem.buffers[id] = { sampleRate: WebAudioSystem.context.sampleRate, buffer: buffer }; WebAudioSystem.playSound(id, on_finish); }); return; } } else { WebAudioSystem._unlock(); } var source = WebAudioSystem.sources[id]; if (source) { if (source.start) { source.start(0); } else { source.noteOn(0); } return; } source = WebAudioSystem.context.createBufferSource(); source.buffer = buffer.buffer; source.connect(WebAudioSystem.context.destination); if (source.start) { source.start(0); } else { source.noteOn(0); } WebAudioSystem.sources[id] = source; setTimeout(function() { WebAudioSystem.stopSound(id); if (on_finish) { on_finish(); } }, buffer.buffer.duration * 1000); }, stopSound: function(id) { var source = WebAudioSystem.sources[id]; if (!source) { return; } if (source.stop) { source.stop(0); } else { source.noteOff(0); } source.disconnect(0); delete(WebAudioSystem.sources[id]); } }; /*! * MediaElement.js * HTML5