2017-01-29 16:46:52 -07:00
|
|
|
define(["playbackManager","events","serverNotifications","connectionManager"],function(playbackManager,events,serverNotifications,connectionManager){"use strict";function getActivePlayerId(){var info=playbackManager.getPlayerInfo();return info?info.id:null}function sendPlayCommand(apiClient,options,playType){var sessionId=getActivePlayerId(),ids=options.ids||options.items.map(function(i){return i.Id}),remoteOptions={ItemIds:ids.join(","),PlayCommand:playType};return options.startPositionTicks&&(remoteOptions.startPositionTicks=options.startPositionTicks),apiClient.sendPlayCommand(sessionId,remoteOptions)}function sendPlayStateCommand(apiClient,command,options){var sessionId=getActivePlayerId();apiClient.sendPlayStateCommand(sessionId,command,options)}return function(){function getCurrentApiClient(){return currentServerId?connectionManager.getApiClient(currentServerId):connectionManager.currentApiClient()}function sendCommandByName(name,options){var command={Name:name};options&&(command.Arguments=options),self.sendCommand(command)}function onPollIntervalFired(){var apiClient=getCurrentApiClient();apiClient.isWebSocketOpen()||apiClient&&apiClient.getSessions().then(function(sessions){processUpdatedSessions(sessions,apiClient)})}function unsubscribeFromPlayerUpdates(){self.isUpdating=!0;var apiClient=getCurrentApiClient();apiClient.isWebSocketOpen()&&apiClient.sendWebSocketMessage("SessionsStop"),pollInterval&&(clearInterval(pollInterval),pollInterval=null)}function getPlayerState(session){return session}function normalizeImages(state){if(state&&state.NowPlayingItem){var item=state.NowPlayingItem;item.ImageTags&&item.ImageTags.Primary||item.PrimaryImageTag&&(item.ImageTags=item.ImageTags||{},item.ImageTags.Primary=item.PrimaryImageTag),item.BackdropImageTag&&item.BackdropItemId===item.Id&&(item.BackdropImageTags=[item.BackdropImageTag]),item.BackdropImageTag&&item.BackdropItemId!==item.Id&&(item.ParentBackdropImageTags=[item.BackdropImageTag],item.ParentBackdropItemId=item.BackdropItemId)}}function firePlaybackEvent(name,session){var state=getPlayerState(session);normalizeImages(state),self.lastPlayerData=state,events.trigger(self,name,[state])}function processUpdatedSessions(sessions,apiClient){var serverId=apiClient.serverId();sessions.map(function(s){s.NowPlayingItem&&(s.NowPlayingItem.ServerId=serverId)});var currentTargetId=getActivePlayerId(),session=sessions.filter(function(s){return s.Id===currentTargetId})[0];session&&(firePlaybackEvent("statechange",session),firePlaybackEvent("timeupdate",session),firePlaybackEvent("pause",session))}var self=this;self.name="Remote Control",self.type="mediaplayer",self.isLocalPlayer=!1,self.id="remoteplayer";var currentServerId;self.sendCommand=function(command){var sessionId=getActivePlayerId(),apiClient=getCurrentApiClient();apiClient.sendCommand(sessionId,command)},self.play=function(options){var playOptions={};return playOptions.ids=options.ids||options.items.map(function(i){return i.Id}),options.startPositionTicks&&(playOptions.startPositionTicks=options.startPositionTicks),sendPlayCommand(getCurrentApiClient(),playOptions,"PlayNow")},self.shuffle=function(item){sendPlayCommand(getCurrentApiClient(),{ids:[item.Id]},"PlayShuffle")},self.instantMix=function(item){sendPlayCommand(getCurrentApiClient(),{ids:[item.Id]},"PlayInstantMix")},self.queue=function(options){sendPlayCommand(getCurrentApiClient(),options,"PlayNext")},self.queueNext=function(options){sendPlayCommand(getCurrentApiClient(),options,"PlayLast")},self.canPlayMediaType=function(mediaType){return mediaType=(mediaType||"").toLowerCase(),"audio"===mediaType||"video"===mediaType},self.canQueueMediaType=function(mediaType){return self.canPlayMediaType(mediaType)},self.stop=function(){sendPlayStateCommand(getCurrentApiClient(),"stop")},self.nextTrack=function(){sendPlayStateCommand(getCurrentApiClient(),"nextTrack")},self.previousTrack=function(){sendPlayStateCommand(getCurrentApiClient(),"previousTrack")},self.seek=function(positionTicks){sendPlayStateCommand(getCurrentApiClient(),"seek",{SeekPositionTicks:positionTick
|