/*********************************************************************** media.js ************************************************************************ Do not change this file unless it should change for all users. This file defines functions that deal with playing of video and audio ************************************************************************/ /********************************************* Function: createAudioString Indata: an array with links to the videos (hopefully same sound file in different formats) Outdata: a string with the right encoding and tags to play video in a variety of formats *********************************************/ var glob_audioID = "audioplayer"; Content.prototype.createAudioString = function(arrayWithFileNames, audioIdString){ var c = this; //Save this to check if we're on the right page later var curPageID = c.currentPageItem.pageid; var audioString = ""; //Creates the unique name of the audio id var idString = 'audioID_' + curPageID; //glob_audioID = idString; audioString += ""; //Create the script to deal with the audio object audioString += '"; return audioString; }; //Function that is triggered after the file loads (onReady - does this trigger more than once with the same audio file?) //Checks if the page the audio was started on is the same as we're currently on (prevents the sound button from appearing on the wrong page) function checkAudioBeforePlaying(audioID){ var c = content; //Get current page id var curPageID = c.currentPageItem.pageid; //Turn the page id into an audio string var currString = 'audioID_' + curPageID; //Check if the audioID(the page when the audio was connected) is the same as currString (the current page) if(audioID == currString){ showSoundButton(); } else { hideSoundButton(); stopAudio(); } } function stopAudio(){ var audioID = "audioplayer"; var playerInstance = jwplayer(audioID); if(playerInstance == null){ //No player exists. return; } try{ playerInstance.stop(); } catch(e){ if(window.console){ console.log("Error with stopAudio function: " + e); } } } function hideSoundButton(){ $("#soundBtn").css('visibility', 'hidden'); } function showSoundButton(){ $("#soundBtn").css('visibility', 'visible'); } function setupError(idString){ if(window.console){ console.log("setupError with jwplayer: " + idString); } } Content.prototype.loadAudio = function(audioObjects){ var audioID = "audioplayer"; var playerInstance = jwplayer(audioID); playerInstance.load([{ file: audioObjects, }]); playerInstance.play(); } Content.prototype.setupAudio = function(){ var c = this;//Connect to our content //Create an array with audio objects var audioDataObjects = $(".audiofile"); if(!audioDataObjects.length){ return; //We don't have any audio files here. } var audioObjects=new Array(); for(var aInd = 0; aInd < audioDataObjects.length;aInd++){ //Find the img files that have a src (these hold our audio files) audioObjects[aInd] = $(audioDataObjects[aInd]).attr("src"); } var audioID = "audioplayer"; //Check if audioplayer exists var jwPlayerObject = jwplayer(audioID); var playerExists = false; if(jwPlayerObject == null){ if(window.console){ console.log("no jwplayer audio player exists");} } else { playerExists = true; } var audioPlayer = $("#" + audioID); if(audioPlayer.length && playerExists){ //Stop playing current audio var jwPlayerObject = jwplayer(audioID); try{ jwPlayerObject.stop(); } catch(e){ if(window.console){ console.log("problem with setupAudio: " + e); } } c.loadAudio(audioObjects); } else { //If no audio player exists, create one var audiohtml = c.createAudioString(audioObjects, audioID); //attach the player to main (so that it isn't erased before play triggers) $("#at_body_container").prepend("
"); $(audioDataObjects).parent().remove(); $("#"+audioID).html(audiohtml); } } /********************************************* Function: resolveAudio Indata: dataObject *********************************************/ Content.prototype.resolveAudio = function(audioInObject){ var c = this; var audioFileExists = false; //Create sound var audioDataObjects = $(audioInObject).find(".audio-data"); if(!audioDataObjects.length){ audioDataObjects = $(audioInObject).find(".audiofile"); } var audioObjects=new Array(); for(var aInd = 0; aInd < audioDataObjects.length;aInd++){ audioObjects[aInd] = $(audioDataObjects[aInd]).attr("src"); } //Create a unique ID var audioID = 'audio_'+tempID; //Create the audio string var audiohtml = c.createAudioString(audioObjects, audioID); //attach the player to main (so that it isn't erased before play triggers) $("#section-3").prepend("
"); $("#"+audioID).html(audiohtml); //Force playback /*playerStatusUpdate = setInterval(function(){ if(jwplayer().getState() == "PLAYING"){ clearInterval(playerStatusUpdate); }else{ if(jwplayer().getState() != "PLAYING" && jwplayer().getPosition() < jwplayer().getDuration() ){ jwplayer().play(true); clearInterval(playerStatusUpdate); } } },100);*/ //Show the audio button //$("#soundBtn").css('visibility', 'visible'); }; /** function playAudio Spelar upp audio-ljud */ Content.prototype.playAudio = function(state,idstring){ var c = this; var soundObject = $("object, audio, video"); var check = false; //Tvinga till true för att fungera på iPad //var isValid = validSoundObject($(soundObject)); var isValid = true; if(idstring == undefined){ if(window.console){ console.log(jwplayer().getState()); console.log("Is valid: "+isValid); console.log("State: "+state); } if(jwplayer().getState() == "IDLE" && jwplayer().getPosition() > 0) return; if(isValid){ if(state){ try{ //glob_audioID not necessary //jwplayer(glob_audioID).play(state); jwplayer().play(state); } catch(e){ if(window.console){ console.log("(1) Having problems in playAudio jwplayer id: " + glob_audioID); } } }else{ try{ //glob_audioID not necessary //jwplayer(glob_audioID).play(); jwplayer().play(state); } catch(e){ if(window.console){ console.log("(2) Having problems in playAudio jwplayer id: " + glob_audioID); } } } check = true; } }else{ if(window.console){ console.log(jwplayer(idstring).getState()); console.log("Is valid 2: "+isValid); console.log("State: "+state); } if(jwplayer(idstring).getState() == "IDLE" && jwplayer(idstring).getPosition() > 0) return; if(isValid){ if(state){ try{ //glob_audioID not necessary //jwplayer(glob_audioID).play(state); jwplayer(idstring).play(state); } catch(e){ if(window.console){ console.log("(1) Having problems in playAudio jwplayer id: " + glob_audioID); } } }else{ try{ //glob_audioID not necessary //jwplayer(glob_audioID).play(); jwplayer(idstring).play(state); } catch(e){ if(window.console){ console.log("(2) Having problems in playAudio jwplayer id: " + glob_audioID); } } } check = true; } } return check; } function validSoundObject(obj){ var id; var rVal = false; //return value $(obj).each(function(i,v){ id = $(this).attr("id"); if($(this).attr("id").indexOf("audioID") != -1){ rVal = true; return false; } }); return rVal; } Content.prototype.setSound = function(){ var c = this; //Unbind the button (remove any click stuff) $("#soundBtn").unbind(); //Add click stuff c.soundButtonFunction(); //set the images setSoundImages(); } Content.prototype.soundButtonFunction = function(){ $("#soundBtn").click(function(e){ //Unbind the button immediately on click to prevent double clicks $("#soundBtn").unbind(); //Toggle the sound toggleSound(); }); } /*Changed to start the audio if it isn't playing or pause if it is playing. NO autostart! Only controls audio!*/ function toggleSound(){ var c = content; var idString; //Get audio objects var audioObjects = $('.audioPlayer'); if(audioObjects.length){ //We have audioobjects $(audioObjects).each(function(){ //Hopefully we only have one, but might as well do this for all of them. //Get the audio state var audioState = jwplayer().getState(); if(audioState == undefined){ var idString = $(this).find("div").filter(":first").attr("id"); audioState = jwplayer(idString).getState(); } if(audioState == "IDLE" || audioState == "PAUSED" ){ c.playAudio(true,idString); setSoundImages('on'); } else if( audioState == "PLAYING"){ c.playAudio(false,idString); setSoundImages('off'); } }); } //Set toggle button again, Prevents double clicks c.soundButtonFunction(); } Content.prototype.ToggleSpeaker = function(skip){ var speakerState = getCookie("speakerState"); if(jwplayer(audioID).getState() == "PAUSED"){ setCookie("speakerState","off",360); $("#soundBtn").css("background-image","url(images/btn_sound_off.png)"); }else{ setCookie("speakerState","on",360); $("#soundBtn").css("background-image","url(images/btn_sound_on.png)"); } }; //Sets the images for the sound image, including mouse in and mouse out //This should be called the first time the page loads as well as each toggle function setSoundImages(){ $("#soundBtn").unbind("mouseenter"); $("#soundBtn").unbind("mouseleave"); var off_normal = "url(images/btn_sound_off.png)"; var off_hover = "url(images/btn_sound_off_down.png)"; var on_normal = "url(images/btn_sound_on.png)"; var on_hover = "url(images/btn_sound_on_down.png)"; var speakerState = getCookie("speakerState"); if(speakerState == "off"){ $("#soundBtn").css("background-image",off_normal); $("#soundBtn").mouseenter(function(e){ $(this).css("background-image",off_hover); }); $("#soundBtn").mouseleave(function(e){ $(this).css("background-image",off_normal); }); } else{ $("#soundBtn").css("background-image",on_normal); $("#soundBtn").mouseenter(function(e){ $(this).css("background-image",on_hover); }); $("#soundBtn").mouseleave(function(e){ $(this).css("background-image",on_normal); }); } }