/* PLAYER CONTROLLER
* - controls the html elements of the radio player (playback/volume buttons)
* - propagates player actions to the player object (play/pause/mute/unmute/setVolume)
* - player object can be FlashPlayer or HtmlPlayer
**************************************************************************************/
window.YnetAudioPlayer = {};
window.YnetAudioPlayerHtmlLoaded=0;
YnetAudioPlayer.PlayerController = function(jqContainer) {
this._container = jqContainer;
// supported button actions
this._buttonActions = ['audioplayer_play', 'audioplayer_pause', 'audioplayer_mute', 'audioplayer_unmute'];
//this._init();
};
YnetAudioPlayer.PlayerController.prototype = {
onPlayerLoad : function(player) {
var self = this;
this._player = this._getPlayerInstance();
// player buttons onclick initialization
this._container.find('.audioplayer_playback .audioplayer_button, .audioplayer_volume .audioplayer_on-off .audioplayer_button').click(function(){
self._onButtonClick(this);
});
this._container.find('.audioplayer_volume .audioplayer_level .audioplayer_level_button').click(function(event){
self._onVolumeLevelClick(this);
});
// show player container
setTimeout(function(){
self._container.find('.player-container').css('opacity', 1);
}, 130);
if(this._player.getStatus().muted) {
this._mute();
} else {
this._setVolume(this._getVolume());
};
//activate player ticker title
var audioPlayerTitle=this._container.find('.audioplayer_title');
var audioPlayerContainer=this._container.find('.audioplayer_title_container');
var audioPlayerTitleWidth = audioPlayerTitle.width();
var audioPlayerContainerWidth = audioPlayerContainer.width();
var audioPlayerGutter=this._container.find('.audioplayer_gutter');
this._player.ticker = 1;
right = 1;
if (!navigator.userAgent.match(/(iPod|iPhone|iPad)/)){
yq(audioPlayerGutter).css('width',(audioPlayerContainerWidth-180)+'px');
} else {
yq(audioPlayerGutter).css('width',(audioPlayerContainerWidth-100)+'px');
}
function tick() {
if(--self._player.ticker < -audioPlayerTitleWidth){
self._player.ticker = 1;
}
audioPlayerTitle.css('right', self._player.ticker + 'px');
setTimeout(tick, 50);
};
if (audioPlayerContainerWidth < audioPlayerTitleWidth) {
tick();
};
},
_getPlayerInstance : function() {
if (this._container.find('audio').length) {
return new YnetAudioPlayer.HtmlPlayer(this._container);
} else {
return flowplayer(this._container.find("div[id^='yntfpcontainer']").attr("id"));
}
},
/* event handlers
************************************************/
// player events
_onClipFinish : function() {
this._toggleButton('audioplayer_pause');
},
// html button events
_onButtonClick : function(domButton) {
var yqButton = yq(domButton);
var action = yqButton.attr('class').split(' ')[1];
if (yq.inArray(action, this._buttonActions) >= 0) {
this['_' + action]();
};
},
_onVolumeLevelClick : function(domButton) {
var level = parseInt(yq(domButton).attr('level'));
this._setVolume(level);
},
_onFlashFail : function() {
this._container.find('.player-container')
.hide()
.before('
');
},
/* player control functions
************************************************/
_audioplayer_play : function() {
this._toggleButton('audioplayer_play');
this._player.play();
},
_audioplayer_pause : function() {
this._toggleButton('audioplayer_pause');
this._player.pause();
},
_audioplayer_mute : function() {
this._toggleButton('audioplayer_mute');
this._setVolumeLevelIndicator(0);
this._player.mute();
this._muted = true;
},
_audioplayer_unmute : function() {
this._toggleButton('audioplayer_unmute');
this._setVolumeLevelIndicator(this._currentVolumeLevel);
this._player.unmute(this._currentVolumeLevel);
this._muted = false;
},
_setVolume : function(level) {
if(this._muted && level > 0) {
this._audioplayer_unmute();
};
this._setVolumeLevelIndicator(level);
this._player.setVolume(Math.round(level*100/10));
this._currentVolumeLevel = level;
},
_getVolume : function() {
return Math.round(this._player.getVolume()*10/100);
},
/* html button manipulation
************************************************/
// play <--> pause
// mute <--> unmute
// button can be string or dom element
_toggleButton : function(button) {
if (typeof button == 'string') {
var yqButton = this._container.find('.audioplayer_button.' + button);
} else {
var yqButton = yq(button);
};
yqButton.hide().siblings().show();
},
_setVolumeLevelIndicator : function(level) {
if (level > 0) {
yqButton = this._container.find('.audioplayer_volume .audioplayer_level .audioplayer_level_button[level=' + level + ']');
yqButton.addClass('on');
yqButton.prevAll().addClass('on');
yqButton.nextAll().removeClass('on');
} else {
this._container.find('.audioplayer_volume .audioplayer_level .audioplayer_level_button').removeClass('on');
};
}
};
/* HTML PLAYER
* - controls the html5 audioElement
************************************************/
YnetAudioPlayer.HtmlPlayer = function(container) {
this._player = container.find('audio').get(0);
this._container=container;
this._init();
var self=this;
this._player.addEventListener("loadedmetadata", function(_event) {
var total = parseInt(self._player.duration, 10);
var totalmins = Math.floor(total/60,10);
var totalsecs = total - totalmins*60;
var timeleft = self._container.find('.audioplayer_timeleft');
timeleft.text(totalmins +':'+totalsecs +' / '+ totalmins +':'+totalsecs);
});
};
YnetAudioPlayer.HtmlPlayer.prototype = {
_init: function (){
var self = this;
this._player.volume=0.5;
var loaded=0;
var _slider_bar=this._container.find('.audioplayer_gutter');
var loadingIndicator = this._container.find('.audioplayer_loading');
var positionIndicator = this._container.find('.audioplayer_handle');
var playedIndicator = this._container.find('.audioplayer_played');
var loundnessIndicator = this._container.find('.audioplayer_loudness');
var timeleft = this._container.find('.audioplayer_timeleft');
var manualSeek=0;
yq(this._player).bind('timeupdate', function() {
var rem = parseInt(self._player.duration - self._player.currentTime, 10);
var total = parseInt(self._player.duration, 10);
var totalmins = Math.floor(total/60,10);
var totalsecs = total - totalmins*60;
var pos = (self._player.currentTime / self._player.duration) * 100;
var mins = Math.floor(rem/60,10);
var secs = rem - mins*60;
timeleft.text(totalmins +':'+totalsecs +' / '+ mins + ':' + (secs > 9 ? secs : '0' + secs));
if (!manualSeek) {
positionIndicator.css({left: pos + '%'});
playedIndicator.css({width: pos + '%'});
}
if (!self._sliderLoaded) {
_slider_bar.slider({
value: 0,
step: 0.01,
orientation: "horizontal",
range: "min",
max: self._player.duration,
animate: true,
slide: function() {
manualSeek = true;
},
stop:function(e,ui) {
manualSeek = false;
self._player.currentTime = ui.value;
}
});
self._sliderLoaded = true;
}
if (self._player.ended) {
ga('audioTracker.send', {
'hitType': 'event',
'eventCategory': dcPath,
'eventAction': 'Finish',
'eventLabel': yq(this._player).find('source').attr('src'),
'eventValue': self._player.duration - self._player.currentTime
});
self._container.find('.audioplayer_button.audioplayer_pause').hide().siblings().show();
pos=0;
positionIndicator.css({left: pos + '%'});
playedIndicator.css({width: pos + '%'});
}
});
if (!window.YnetAudioPlayerHtmlLoaded) {
var path=location.href;
var startId=path.indexOf('L-');
var pathId=path.substr(startId+2,path.length-1);
var endId=pathId.indexOf(',');
var showId=pathId.substr(0,endId);
var UgooglePrefix='';
if (path.indexOf('/home')>-1) {
UgooglePrefix = "h";
} else if (path.indexOf('/article')>-1) {
UgooglePrefix = "a";
} else {
UgooglePrefix = "o";
}
if (navigator.userAgent.match(/(iPod|iPhone|iPad|Android|BlackBerry|IEMobile)/)) {
ga('create', 'UA-17826742-22', 'realcommerce.co.il', {'name': 'audioTracker'});
ga('audioTracker.send', {'hitType': 'pageview','page': path+'?prof='+showId+'.ECentral','title': UgooglePrefix+showId+' - '+document.title});
}
}
},
play : function() {
this._player.play();
ga('audioTracker.send', {
'hitType': 'event',
'eventCategory': dcPath,
'eventAction': 'Play',
'eventLabel': yq(this._player).find('source').attr('src'),
'eventValue': this._player.duration - this._player.currentTime
});
},
pause : function() {
this._player.pause();
ga('audioTracker.send', {
'hitType': 'event',
'eventCategory': dcPath,
'eventAction': 'Pause',
'eventLabel': yq(this._player).find('source').attr('src'),
'eventValue': this._player.duration - this._player.currentTime
});
},
mute : function() {
this._player.volume=0;
},
unmute : function(level) {
this.setVolume(level*10);
},
setVolume : function(level) {
this._player.volume=level/100;
},
getVolume : function() {
return this._player.volume * 100;
},
getStatus : function() {
return {
muted: false
};
},
_onTimeUpdate : function() {
}
};
YnetAudioPlayer.utils = {
_getFlashVersion : function() {
var replaceRegexp = new RegExp(String.fromCharCode(92)+'D+','g');
var matchRegexp = new RegExp('^,?(.+),?' + String.fromCharCode(36));
try {
try {
var axo = new ActiveXObject('ShockwaveFlash.ShockwaveFlash.6');
try { axo.AllowScriptAccess = 'always'; }
catch(e) { return '6,0,0'; }
} catch(e) {}
return new ActiveXObject('ShockwaveFlash.ShockwaveFlash').GetVariable('$version').replace(replaceRegexp, ',').match(matchRegexp)[1];
} catch(e) {
try {
if(navigator.mimeTypes["application/x-shockwave-flash"].enabledPlugin){
return (navigator.plugins["Shockwave Flash 2.0"] || navigator.plugins["Shockwave Flash"]).description.replace(replaceRegexp, ',').match(matchRegexp)[1];
}
} catch(e) {}
}
return '0,0,0';
},
hasFlash : function() {
var version = this._getFlashVersion().split(',').shift();
var hasFlash=version=='0'?false:true;
return hasFlash;
}
};