(function (window, document, $, setTimeout, clearTimeout) { var currentPlayer; var currentTimeElement; var nowPlayingImageElement; var nowPlayingTextElement; var unmuteButton; var muteButton; var volumeSlider; var isVolumeSliderActive; var unpauseButton; var pauseButton; var positionSlider; var isPositionSliderActive; var lastPlayerState; function getNowPlayingBarHtml() { var html = ''; html += ''; return html; } function bindEvents(elem) { currentTimeElement = $('.currentTime', elem); nowPlayingImageElement = $('.nowPlayingImage', elem); nowPlayingTextElement = $('.nowPlayingText', elem); unmuteButton = $('.unmuteButton', elem).on('click', function () { if (currentPlayer) { currentPlayer.unMute(); } }); muteButton = $('.muteButton', elem).on('click', function () { if (currentPlayer) { currentPlayer.mute(); } }); $('.stopButton', elem).on('click', function () { if (currentPlayer) { currentPlayer.stop(); } }); pauseButton = $('.pauseButton', elem).on('click', function () { if (currentPlayer) { currentPlayer.pause(); } }); unpauseButton = $('.unpauseButton', elem).on('click', function () { if (currentPlayer) { currentPlayer.unpause(); } }); $('.nextTrackButton', elem).on('click', function () { if (currentPlayer) { currentPlayer.nextTrack(); } }); $('.previousTrackButton', elem).on('click', function () { if (currentPlayer) { currentPlayer.previousTrack(); } }); volumeSlider = $('.volumeSlider', elem).on('slidestart', function () { isVolumeSliderActive = true; }).on('slidestop', function () { isVolumeSliderActive = false; if (currentPlayer) { currentPlayer.setVolume(this.value); } }); positionSlider = $('.positionSlider', elem).on('slidestart', function () { isPositionSliderActive = true; }).on('slidestop', function () { isPositionSliderActive = false; if (currentPlayer && lastPlayerState) { var newPercent = parseFloat(this.value); var newPositionTicks = (newPercent / 100) * lastPlayerState.runtimeTicks; currentPlayer.seek(Math.floor(newPositionTicks)); } }); } function getNowPlayingBar() { var elem = $('.nowPlayingBar'); if (elem.length) { return elem; } elem = $(getNowPlayingBarHtml()).insertBefore('#footerNotifications').trigger('create'); bindEvents(elem); return elem; } function updatePlayerState(state) { if (state.itemName) { showNowPlayingBar(); } else { hideNowPlayingBar(); return; } lastPlayerState = state; if (!muteButton) { getNowPlayingBar(); } if (state.isMuted) { muteButton.hide(); unmuteButton.show(); } else { muteButton.show(); unmuteButton.hide(); } if (state.isPaused) { pauseButton.hide(); unpauseButton.show(); } else { pauseButton.show(); unpauseButton.hide(); } if (!isVolumeSliderActive) { volumeSlider.val(state.volumeLevel || 0).slider('refresh'); } if (!isPositionSliderActive) { if (state.canSeek) { var pct = state.positionTicks / state.runtimeTicks; pct *= 100; positionSlider.val(pct).prop('disabled', ''); } else { positionSlider.val(0).prop('disabled', 'disabled'); } positionSlider.slider('refresh'); } var timeText = Dashboard.getDisplayTime(state.positionTicks); if (state.runtimeTicks) { timeText += " / " + Dashboard.getDisplayTime(state.runtimeTicks); } currentTimeElement.html(timeText); updateNowPlayingInfo(state); } var currentImgUrl; function updateNowPlayingInfo(state) { var nameHtml = MediaPlayer.getNowPlayingNameHtml(state); if (nameHtml.indexOf('
') != -1) { nowPlayingTextElement.addClass('nowPlayingDoubleText'); } else { nowPlayingTextElement.removeClass('nowPlayingDoubleText'); } nowPlayingTextElement.html(nameHtml); var url; if (state.primaryImageTag) { url = ApiClient.getImageUrl(state.primaryImageItemId, { type: "Primary", height: 80, tag: state.primaryImageTag }); } else if (state.backdropImageTag) { url = ApiClient.getImageUrl(state.backdropItemId, { type: "Backdrop", height: 80, tag: state.backdropImageTag, index: 0 }); } else if (state.thumbImageTag) { url = ApiClient.getImageUrl(state.thumbImageItemId, { type: "Thumb", height: 80, tag: state.thumbImageTag }); } else if (state.itemType == "TvChannel" || state.itemType == "Recording") { url = "css/images/items/detail/tv.png"; } else if (state.mediaType == "Audio") { url = "css/images/items/detail/audio.png"; } else { url = "css/images/items/detail/video.png"; } if (url == currentImgUrl) { return; } currentImgUrl = url; nowPlayingImageElement.html(''); } function onPlaybackStart(e, state) { var player = this; player.beginPlayerUpdates(); onStateChanged.call(player, e, state); } function showNowPlayingBar() { var nowPlayingBar = getNowPlayingBar(); nowPlayingBar.show(); } function hideNowPlayingBar() { // Use a timeout to prevent the bar from hiding and showing quickly // in the event of a stop->play command getNowPlayingBar().hide(); } function onPlaybackStopped(e, state) { var player = this; player.endPlayerUpdates(); hideNowPlayingBar(); } function onStateChanged(e, state) { var player = this; if (player.isDefaultPlayer && state.mediaType == 'Video') { return; } updatePlayerState(state); } function releaseCurrentPlayer() { if (currentPlayer) { $(currentPlayer).off('.nowplayingbar'); currentPlayer.endPlayerUpdates(); currentPlayer = null; hideNowPlayingBar(); } } function bindToPlayer(player) { releaseCurrentPlayer(); currentPlayer = player; player.getPlayerState().done(function (state) { if (state.itemName) { player.beginPlayerUpdates(); } onStateChanged.call(player, null, state); }); $(player).on('playbackstart.nowplayingbar', onPlaybackStart) .on('playbackstop.nowplayingbar', onPlaybackStopped) .on('volumechange.nowplayingbar', onStateChanged) .on('playstatechange.nowplayingbar', onStateChanged) .on('positionchange.nowplayingbar', onStateChanged); } $(function () { $(MediaController).on('playerchange', function () { bindToPlayer(MediaController.getCurrentPlayer()); }); bindToPlayer(MediaController.getCurrentPlayer()); }); })(window, document, jQuery, setTimeout, clearTimeout);