jellyfin-web/dashboard-ui/cordova/android/mediasession.js

187 lines
5.1 KiB
JavaScript
Raw Normal View History

2015-06-08 22:56:46 -07:00
(function () {
// Reports media playback to the device for lock screen control
var currentPlayer;
2015-06-11 14:22:44 -07:00
var lastUpdateTime = 0;
2015-06-08 22:56:46 -07:00
2015-06-25 14:50:56 -07:00
function allowLocalPlayer() {
return false;
}
2015-06-08 22:56:46 -07:00
function updatePlayerState(state, eventName) {
if (!state.NowPlayingItem) {
hideMediaControls();
return;
}
2015-06-23 15:13:06 -07:00
var isLocalPlayer = MediaController.getPlayerInfo().isLocalPlayer || false;
// Local players do their own notifications
2015-06-25 14:50:56 -07:00
if (isLocalPlayer && !allowLocalPlayer()) {
2015-06-23 15:13:06 -07:00
return;
}
2015-06-08 22:56:46 -07:00
// dummy this up
if (eventName == 'init') {
eventName = 'positionchange';
}
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-08-16 15:03:22 -07:00
// Switch these two around for video
2015-08-16 21:56:22 -07:00
if (state.NowPlayingItem.MediaType == 'Video' && parts.length > 1) {
2015-08-16 15:03:22 -07:00
var temp = artist;
artist = title;
title = temp;
}
2015-06-08 22:56:46 -07:00
var album = state.NowPlayingItem.Album || '';
2015-06-11 10:57:59 -07:00
var itemId = state.NowPlayingItem.Id;
2015-06-08 22:56:46 -07:00
2015-06-23 15:13:06 -07:00
// Convert to ms
var duration = state.NowPlayingItem.RunTimeTicks ? (state.NowPlayingItem.RunTimeTicks / 10000) : 0;
var position = playState.PositionTicks ? (playState.PositionTicks / 10000) : 0;
2015-06-08 22:56:46 -07:00
var isPaused = playState.IsPaused || false;
var canSeek = playState.CanSeek || false;
var url = '';
2015-06-10 06:37:07 -07:00
var imgHeight = 400;
2015-06-08 22:56:46 -07:00
var nowPlayingItem = state.NowPlayingItem;
if (nowPlayingItem.PrimaryImageTag) {
url = ApiClient.getScaledImageUrl(nowPlayingItem.PrimaryImageItemId, {
type: "Primary",
height: imgHeight,
tag: nowPlayingItem.PrimaryImageTag
});
} else if (nowPlayingItem.ThumbImageTag) {
url = ApiClient.getScaledImageUrl(nowPlayingItem.ThumbImageItemId, {
type: "Thumb",
height: imgHeight,
tag: nowPlayingItem.ThumbImageTag
});
}
else if (nowPlayingItem.BackdropImageTag) {
url = ApiClient.getScaledImageUrl(nowPlayingItem.BackdropItemId, {
type: "Backdrop",
height: imgHeight,
tag: nowPlayingItem.BackdropImageTag,
index: 0
});
}
2015-06-11 10:57:59 -07:00
// Don't go crazy reporting position changes
if (eventName == 'positionchange') {
2015-06-11 14:22:44 -07:00
if (lastUpdateTime) {
// Only report if this item hasn't been reported yet, or if there's an actual playback change.
// Don't report on simple time updates
2015-06-11 10:57:59 -07:00
return;
}
}
2015-06-13 07:46:59 -07:00
MainActivity.updateMediaSession(eventName, isLocalPlayer, itemId, title, artist, album, parseInt(duration), parseInt(position), url, canSeek, isPaused);
2015-06-11 10:57:59 -07:00
lastUpdateTime = new Date().getTime();
2015-06-08 22:56:46 -07:00
}
function onStateChanged(e, state) {
updatePlayerState(state, e.type);
}
function onPlaybackStart(e, state) {
2015-06-26 20:27:38 -07:00
Logger.log('nowplaying event: ' + e.type);
2015-06-08 22:56:46 -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-06-08 22:56:46 -07:00
var player = this;
player.endPlayerUpdates();
hideMediaControls();
}
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-06-08 22:56:46 -07:00
currentPlayer.endPlayerUpdates();
currentPlayer = null;
hideMediaControls();
}
}
function hideMediaControls() {
MainActivity.hideMediaSession();
2015-06-11 14:22:44 -07:00
lastUpdateTime = 0;
2015-06-08 22:56:46 -07:00
}
function bindToPlayer(player) {
releaseCurrentPlayer();
2015-06-25 14:50:56 -07:00
if (player.isLocalPlayer && !allowLocalPlayer()) {
return;
}
2015-06-08 22:56:46 -07:00
currentPlayer = player;
2015-07-02 05:55:07 -07:00
Logger.log('binding remotecontrols to ' + player.name);
2015-06-08 22:56:46 -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-06-08 22:56:46 -07:00
}
Dashboard.ready(function () {
2015-06-26 20:27:38 -07:00
Logger.log('binding remotecontrols to MediaController');
2015-06-08 22:56:46 -07:00
$(MediaController).on('playerchange', function () {
bindToPlayer(MediaController.getCurrentPlayer());
});
bindToPlayer(MediaController.getCurrentPlayer());
});
})();