/* * Copyright (C) 2005-2013 Team XBMC * http://xbmc.org * * This Program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2, or (at your option) * any later version. * * This Program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with XBMC; see the file COPYING. If not, see * . * */ var NowPlayingManager = function () { this.init(); return true; } NowPlayingManager.prototype = { updateCounter: 0, activePlayer: "", activePlayerId: -1, currentItem: -1, playing: false, paused: false, playlistid: -1, init: function () { $('#pbPause').hide(); /* Assume we are not playing something */ this.bindPlaybackControls(); this.updateState(); $('#nextTrack').bind('click', jQuery.proxy(this.showPlaylist, this)); $('#nowPlayingPlaylist').bind('click', function () { return false; }); $(window).bind('click', jQuery.proxy(this.hidePlaylist, this)); }, updateState: function () { xbmc.rpc.request({ 'context': this, 'method': 'Player.GetActivePlayers', 'timeout': 3000, 'success': function (data) { if (data && data.result && data.result.length > 0) { if (data.result[0].playerid != this.activePlayerId) { this.activePlayerId = data.result[0].playerid; this.activePlayer = data.result[0].type; if (this.activePlayer == "audio") { this.stopVideoPlaylistUpdate(); this.displayAudioNowPlaying(); } else if (this.activePlayer == "video") { this.stopAudioPlaylistUpdate(); this.displayVideoNowPlaying(); } else { this.stopVideoPlaylistUpdate(); this.stopAudioPlaylistUpdate(); this.activePlayer = ""; this.activePlayerId = -1; } this.stopRefreshTime(); this.updatePlayer(); } } else if (!data || !data.result || data.result.length <= 0) { this.stopVideoPlaylistUpdate(); this.stopAudioPlaylistUpdate(); this.activePlayer = ""; this.activePlayerId = -1; } if (this.activePlayerId >= 0) { this.showFooter(); } else { this.stopRefreshTime(); this.hideFooter(); } setTimeout(jQuery.proxy(this.updateState, this), 1000); }, 'error': function (data, error) { xbmc.core.displayCommunicationError(); setTimeout(jQuery.proxy(this.updateState, this), 2000); } }); }, updatePlayer: function () { xbmc.rpc.request({ 'context': this, 'method': 'Player.GetProperties', 'params': { 'playerid': this.activePlayerId, 'properties': [ 'playlistid', 'speed', 'position', 'totaltime', 'time' ] }, 'success': function (data) { if (data && data.result) { this.playlistid = data.result.playlistid; this.playing = data.result.speed != 0; this.paused = data.result.speed == 0; this.currentItem = data.result.position; this.trackBaseTime = xbmc.core.timeToDuration(data.result.time); this.trackDurationTime = xbmc.core.timeToDuration(data.result.totaltime); if (!this.autoRefreshAudioData && !this.autoRefreshVideoData && this.playing) { if (this.activePlayer == 'audio') { this.autoRefreshAudioData = true; this.refreshAudioData(); } else if (this.activePlayer == 'video') { this.autoRefreshVideoData = true; this.refreshVideoData(); } } } if ((this.autoRefreshAudioData || this.autoRefreshVideoData) && !this .activeItemTimer) { this.activeItemTimer = 1; setTimeout(jQuery.proxy(this.updateActiveItemDurationLoop, this), 1000); } } }); }, bindPlaybackControls: function () { $('#pbNext').bind('click', jQuery.proxy(this.nextTrack, this)); $('#pbPrev').bind('click', jQuery.proxy(this.prevTrack, this)); $('#pbStop').bind('click', jQuery.proxy(this.stopTrack, this)); $('#pbPlay').bind('click', jQuery.proxy(this.playPauseTrack, this)); $('#pbPause').bind('click', jQuery.proxy(this.playPauseTrack, this)); that = this $(document).keypress(function (event) { switch (event.which) { case 32: //spacebar event.preventDefault() jQuery.proxy(that.playPauseTrack, that)(); break; case 120: //x key event.preventDefault() jQuery.proxy(that.stopTrack, that)(); break; case 44: //period key event.preventDefault() jQuery.proxy(that.nextTrack, that)(); break; case 46: //comma key event.preventDefault() jQuery.proxy(that.prevTrack, that)(); break; } }); }, showPlaylist: function () { $('#nextText').html('Playlist: '); $('#nowPlayingPlaylist').show(); return false; }, hidePlaylist: function () { $('#nextText').html('Next: '); $('#nowPlayingPlaylist').hide(); return false; }, hideFooter: function () { $('#footerPopover').hide(); $('#overlay').css('bottom', '0px'); }, showFooter: function () { $('#footerPopover').show(); $('#overlay').css('bottom', '150px'); }, nextTrack: function () { if (this.activePlayer) { xbmc.rpc.request({ 'method': 'Player.GoTo', 'params': { 'playerid': this.activePlayerId, 'to': 'next' }, 'success': function () {} }); } }, prevTrack: function () { if (this.activePlayer) { xbmc.rpc.request({ 'method': 'Player.GoTo', 'params': { 'playerid': this.activePlayerId, 'to': 'previous' }, 'success': function () {} }); } }, stopTrack: function () { if (this.activePlayer) { xbmc.rpc.request({ 'context': this, 'method': 'Player.Stop', 'params': { 'playerid': this.activePlayerId }, 'success': function (data) { if (data && data.result == 'OK') { this.playing = false; this.paused = false; this.trackBaseTime = 0; this.trackDurationTime = 0; this.showPlayButton(); } } }); } }, playPauseTrack: function () { if (this.activePlayer) { var method = ((this.playing || this.paused) ? 'Player.PlayPause' : 'Playlist.Play'); xbmc.rpc.request({ 'context': this, 'method': method, 'params': { 'playerid': this.activePlayerId }, 'success': function (data) { if (data && data.result) { this.playing = data.result.speed != 0; this.paused = data.result.speed == 0; if (this.playing) { this.showPauseButton(); } else { this.showPlayButton(); } } } }); } }, showPauseButton: function () { $('#pbPause').show(); $('#pbPlay').hide(); }, showPlayButton: function () { $('#pbPause').hide(); $('#pbPlay').show(); }, displayAudioNowPlaying: function () { if (!this.autoRefreshAudioPlaylist) { this.autoRefreshAudioPlaylist = true; this.updateAudioPlaylist(); } }, displayVideoNowPlaying: function () { if (!this.autoRefreshVideoPlaylist) { this.autoRefreshVideoPlaylist = true; this.updateVideoPlaylist(); } }, playPlaylistItem: function (sender) { var sequenceId = $(sender.currentTarget).attr('seq'); if (!this.activePlaylistItem || (this.activePlaylistItem !== undefined && sequenceId != this .activePlaylistItem.seq)) { xbmc.rpc.request({ 'method': 'Player.GoTo', 'params': { 'playerid': this.activePlayerId, 'to': sequenceId }, 'success': function () {} }); } this.hidePlaylist(); }, playlistChanged: function (newPlaylist) { if (this.activePlaylist && !newPlaylist || !this.activePlaylist && newPlaylist) { return true; } if (!this.activePlaylist && !newPlaylist) { return false; } if (this.activePlaylist.length != newPlaylist.length) { return true; } for (var i = 0; i < newPlaylist.length; i++) { if (!this.comparePlaylistItems(this.activePlaylist[i], newPlaylist[i])) { return true; } } return false; }, updateAudioPlaylist: function () { xbmc.rpc.request({ 'context': this, 'method': 'Playlist.GetItems', 'params': { 'playlistid': this.playlistid, 'properties': [ 'title', 'album', 'artist', 'duration', 'thumbnail' ] }, 'success': function (data) { if (data && data.result && data.result.items && data.result.items.length > 0 && data .result.limits.total > 0) { if (!this.activePlaylistItem || this.playlistChanged(data.result.items) || (this .activePlaylistItem && (this.activePlaylistItem.seq != this.currentItem) )) { var ul = $('