jellyfin-web/dashboard-ui/cordova/remotecontrols.js

189 lines
4.8 KiB
JavaScript
Raw Normal View History

2015-05-11 12:59:59 -07:00
(function () {
// Reports media playback to the device for lock screen control
var currentPlayer;
var lastPlayerState;
function updatePlayerState(state) {
if (!state.NowPlayingItem) {
hideNowPlayingBar();
return;
}
lastPlayerState = state;
var playState = state.PlayState || {};
var nameHtml = MediaController.getNowPlayingNameHtml(state.NowPlayingItem) || '';
var parts = nameHtml.split('<br/>');
var artist = parts.length == 1 ? '' : parts[0];
var title = parts[parts.length - 1];
2015-06-04 13:27:46 -07:00
var album = state.NowPlayingItem.Album || '';
var duration = state.NowPlayingItem.RunTimeTicks ? (state.NowPlayingItem.RunTimeTicks / 10000000) : 0;
2015-05-11 12:59:59 -07:00
var elapsedTime = playState.PositionTicks ? (playState.PositionTicks / 10000000) : 0;
var url = '';
2015-07-06 19:25:23 -07:00
var imgHeight = 600;
2015-05-11 12:59:59 -07:00
var nowPlayingItem = state.NowPlayingItem;
if (nowPlayingItem.PrimaryImageTag) {
url = ApiClient.getScaledImageUrl(nowPlayingItem.PrimaryImageItemId, {
type: "Primary",
height: imgHeight,
tag: nowPlayingItem.PrimaryImageTag
});
}
else if (nowPlayingItem.BackdropImageTag) {
url = ApiClient.getScaledImageUrl(nowPlayingItem.BackdropItemId, {
type: "Backdrop",
height: imgHeight,
tag: nowPlayingItem.BackdropImageTag,
index: 0
});
} else if (nowPlayingItem.ThumbImageTag) {
url = ApiClient.getScaledImageUrl(nowPlayingItem.ThumbImageItemId, {
type: "Thumb",
height: imgHeight,
tag: nowPlayingItem.ThumbImageTag
});
}
var params = [artist, title, album, url, duration, elapsedTime];
2015-05-12 21:55:19 -07:00
try {
window.remoteControls.updateMetas(onUpdateMetasSuccess, onUpdateMetasFail, params);
} catch (err) {
onUpdateMetasFail(err);
}
2015-05-11 12:59:59 -07:00
}
2015-07-08 17:20:01 -07:00
var lastUpdateTime = 0;
2015-05-11 12:59:59 -07:00
function onStateChanged(e, state) {
2015-07-08 17:20:01 -07:00
if (e.type == 'positionchange') {
// Try to avoid hammering the document with changes
var now = new Date().getTime();
if ((now - lastUpdateTime) < 700) {
return;
}
lastUpdateTime = now;
}
2015-05-11 12:59:59 -07:00
updatePlayerState(state);
}
function onPlaybackStart(e, state) {
2015-06-26 20:27:38 -07:00
Logger.log('nowplaying event: ' + e.type);
2015-05-11 12:59:59 -07:00
var player = this;
player.beginPlayerUpdates();
onStateChanged.call(player, e, state);
}
function onPlaybackStopped(e, state) {
2015-06-26 20:27:38 -07:00
Logger.log('nowplaying event: ' + e.type);
2015-05-11 12:59:59 -07:00
var player = this;
player.endPlayerUpdates();
hideNowPlayingBar();
}
function releaseCurrentPlayer() {
if (currentPlayer) {
2015-06-29 11:45:42 -07:00
$(currentPlayer).off('playbackstart', onPlaybackStart)
.off('playbackstop', onPlaybackStopped)
.off('playstatechange', onStateChanged)
.off('positionchange', onStateChanged);
2015-05-11 12:59:59 -07:00
currentPlayer.endPlayerUpdates();
currentPlayer = null;
hideNowPlayingBar();
}
}
function hideNowPlayingBar() {
var artist = "";
var title = "";
var album = "";
var image = "";
var duration = 0;
var elapsedTime = 0;
var params = [artist, title, album, image, duration, elapsedTime];
2015-05-12 21:55:19 -07:00
try {
window.remoteControls.updateMetas(onUpdateMetasSuccess, onUpdateMetasFail, params);
} catch (err) {
onUpdateMetasFail(err);
}
2015-05-11 12:59:59 -07:00
}
function onUpdateMetasSuccess() {
2015-06-26 20:27:38 -07:00
Logger.log('onUpdateMetasSuccess');
2015-05-11 12:59:59 -07:00
}
function onUpdateMetasFail(fail) {
2015-06-26 20:27:38 -07:00
Logger.log('onUpdateMetasFail: ' + fail);
2015-05-11 12:59:59 -07:00
}
function bindToPlayer(player) {
releaseCurrentPlayer();
currentPlayer = player;
if (!player.isLocalPlayer) {
return;
}
2015-06-26 20:27:38 -07:00
Logger.log('binding remotecontrols to MediaPlayer');
2015-05-12 21:55:19 -07:00
2015-05-11 12:59:59 -07:00
player.getPlayerState().done(function (state) {
if (state.NowPlayingItem) {
player.beginPlayerUpdates();
}
onStateChanged.call(player, { type: 'init' }, state);
});
2015-06-29 11:45:42 -07:00
$(player).on('playbackstart', onPlaybackStart)
.on('playbackstop', onPlaybackStopped)
.on('playstatechange', onStateChanged)
.on('positionchange', onStateChanged);
2015-05-11 12:59:59 -07:00
}
2015-05-19 12:15:40 -07:00
Dashboard.ready(function () {
2015-05-11 12:59:59 -07:00
2015-06-26 20:27:38 -07:00
Logger.log('binding remotecontrols to MediaController');
2015-05-12 21:55:19 -07:00
2015-05-19 12:15:40 -07:00
$(MediaController).on('playerchange', function () {
2015-05-11 12:59:59 -07:00
bindToPlayer(MediaController.getCurrentPlayer());
});
2015-05-19 12:15:40 -07:00
bindToPlayer(MediaController.getCurrentPlayer());
});
2015-05-11 12:59:59 -07:00
})();