2019-01-23 11:33:34 +00:00
|
|
|
define(['playbackManager', 'nowPlayingHelper', 'events', 'connectionManager'], function (playbackManager, nowPlayingHelper, events, connectionManager) {
|
2018-10-23 01:05:09 +03:00
|
|
|
"use strict";
|
2019-03-21 21:54:36 +00:00
|
|
|
|
2019-03-17 00:31:56 +00:00
|
|
|
// no support for mediaSession
|
|
|
|
if (!navigator.mediaSession && !window.NativeShell) {
|
|
|
|
return;
|
|
|
|
}
|
2018-10-23 01:05:09 +03:00
|
|
|
|
2019-01-10 15:39:37 +03:00
|
|
|
// Reports media playback to the device for lock screen control
|
|
|
|
|
|
|
|
var currentPlayer;
|
|
|
|
var lastUpdateTime = 0;
|
|
|
|
|
2020-04-15 07:54:33 +02:00
|
|
|
function seriesImageUrl(item, options = {}) {
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
if (item.Type !== 'Episode') {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
options.type = options.type || "Primary";
|
|
|
|
|
|
|
|
if (options.type === 'Primary') {
|
|
|
|
|
|
|
|
if (item.SeriesPrimaryImageTag) {
|
|
|
|
|
|
|
|
options.tag = item.SeriesPrimaryImageTag;
|
|
|
|
|
|
|
|
return connectionManager.getApiClient(item.ServerId).getScaledImageUrl(item.SeriesId, options);
|
|
|
|
}
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
if (options.type === 'Thumb') {
|
|
|
|
|
|
|
|
if (item.SeriesThumbImageTag) {
|
|
|
|
|
|
|
|
options.tag = item.SeriesThumbImageTag;
|
|
|
|
|
|
|
|
return connectionManager.getApiClient(item.ServerId).getScaledImageUrl(item.SeriesId, options);
|
|
|
|
}
|
|
|
|
if (item.ParentThumbImageTag) {
|
|
|
|
|
|
|
|
options.tag = item.ParentThumbImageTag;
|
|
|
|
|
|
|
|
return connectionManager.getApiClient(item.ServerId).getScaledImageUrl(item.ParentThumbItemId, options);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
|
2020-04-15 07:54:33 +02:00
|
|
|
function imageUrl(item, options = {}) {
|
2019-01-10 15:39:37 +03:00
|
|
|
options.type = options.type || "Primary";
|
|
|
|
|
|
|
|
if (item.ImageTags && item.ImageTags[options.type]) {
|
|
|
|
|
|
|
|
options.tag = item.ImageTags[options.type];
|
|
|
|
return connectionManager.getApiClient(item.ServerId).getScaledImageUrl(item.Id, options);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (item.AlbumId && item.AlbumPrimaryImageTag) {
|
|
|
|
|
|
|
|
options.tag = item.AlbumPrimaryImageTag;
|
|
|
|
return connectionManager.getApiClient(item.ServerId).getScaledImageUrl(item.AlbumId, options);
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
|
2020-04-15 07:54:33 +02:00
|
|
|
function pushImageUrl(item, imageOptions = {}) {
|
2019-01-10 15:39:37 +03:00
|
|
|
var url = seriesImageUrl(item, imageOptions) || imageUrl(item, imageOptions);
|
2019-03-17 00:31:56 +00:00
|
|
|
|
2019-01-10 15:39:37 +03:00
|
|
|
if (url) {
|
2019-03-17 00:31:56 +00:00
|
|
|
var height = imageOptions.height || imageOptions.maxHeight;
|
|
|
|
|
2020-04-15 07:54:33 +02:00
|
|
|
return {
|
2019-03-17 00:31:56 +00:00
|
|
|
src: url,
|
|
|
|
sizes: height + 'x' + height
|
2020-04-15 07:54:33 +02:00
|
|
|
};
|
2019-01-10 15:39:37 +03:00
|
|
|
}
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function getImageUrls(item) {
|
|
|
|
var list = [];
|
2020-04-15 07:54:33 +02:00
|
|
|
const imageSizes = [96, 128, 192, 256, 384, 512];
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-04-15 07:54:33 +02:00
|
|
|
imageSizes.forEach((size) => {
|
|
|
|
list.push(pushImageUrl(item, {height: size}));
|
|
|
|
});
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
return list;
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function updatePlayerState(player, state, eventName) {
|
2020-01-26 14:40:32 +01:00
|
|
|
// Don't go crazy reporting position changes
|
2020-01-29 08:38:56 +01:00
|
|
|
if (eventName == 'timeupdate') {
|
2020-01-26 14:40:32 +01:00
|
|
|
// 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
|
|
|
|
return;
|
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2018-10-23 01:05:09 +03:00
|
|
|
var item = state.NowPlayingItem;
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
if (!item) {
|
|
|
|
hideMediaControls();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-03-21 21:54:36 +00:00
|
|
|
if (eventName == 'init') { // transform "init" event into "timeupdate" to restraint update rate
|
2019-03-17 00:31:56 +00:00
|
|
|
eventName = 'timeupdate';
|
|
|
|
}
|
|
|
|
|
|
|
|
var isVideo = item.MediaType === 'Video';
|
|
|
|
var isLocalPlayer = player.isLocalPlayer || false;
|
|
|
|
|
|
|
|
// Local players do their own notifications
|
|
|
|
if (isLocalPlayer && isVideo) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-01-10 15:39:37 +03:00
|
|
|
var playState = state.PlayState || {};
|
|
|
|
var parts = nowPlayingHelper.getNowPlayingNames(item);
|
2020-01-26 14:40:32 +01:00
|
|
|
var artist = parts[parts.length - 1].text;
|
|
|
|
var title = parts.length === 1 ? '' : parts[0].text;
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
var album = item.Album || '';
|
|
|
|
var itemId = item.Id;
|
|
|
|
|
|
|
|
// Convert to ms
|
|
|
|
var duration = parseInt(item.RunTimeTicks ? (item.RunTimeTicks / 10000) : 0);
|
|
|
|
var currentTime = parseInt(playState.PositionTicks ? (playState.PositionTicks / 10000) : 0);
|
|
|
|
|
|
|
|
var isPaused = playState.IsPaused || false;
|
|
|
|
var canSeek = playState.CanSeek || false;
|
|
|
|
|
2020-04-15 07:54:33 +02:00
|
|
|
if ('mediaSession' in navigator) {
|
2019-03-17 00:31:56 +00:00
|
|
|
navigator.mediaSession.metadata = new MediaMetadata({
|
2020-01-26 14:40:32 +01:00
|
|
|
title: title,
|
|
|
|
artist: artist,
|
2019-03-17 00:31:56 +00:00
|
|
|
album: album,
|
2020-04-15 07:54:33 +02:00
|
|
|
artwork: getImageUrls(item)
|
2019-03-17 00:31:56 +00:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
var imageUrl = [];
|
2020-04-15 07:54:33 +02:00
|
|
|
imageUrl.push(pushImageUrl(item));
|
2019-03-17 00:31:56 +00:00
|
|
|
|
|
|
|
if (imageUrl.length) {
|
|
|
|
imageUrl = imageUrl[0].src;
|
|
|
|
} else {
|
|
|
|
imageUrl = null;
|
|
|
|
}
|
2019-03-21 21:54:36 +00:00
|
|
|
|
2019-03-17 00:31:56 +00:00
|
|
|
window.NativeShell.updateMediaSession({
|
|
|
|
action: eventName,
|
|
|
|
isLocalPlayer: isLocalPlayer,
|
|
|
|
itemId: itemId,
|
2020-01-26 14:40:32 +01:00
|
|
|
title: title,
|
|
|
|
artist: artist,
|
2019-03-17 00:31:56 +00:00
|
|
|
album: album,
|
|
|
|
duration: duration,
|
|
|
|
position: currentTime,
|
|
|
|
imageUrl: imageUrl,
|
|
|
|
canSeek: canSeek,
|
|
|
|
isPaused: isPaused
|
|
|
|
});
|
|
|
|
}
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function onGeneralEvent(e) {
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2018-10-23 01:05:09 +03:00
|
|
|
var player = this;
|
2019-01-10 15:39:37 +03:00
|
|
|
var state = playbackManager.getPlayerState(player);
|
|
|
|
|
|
|
|
updatePlayerState(player, state, e.type);
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function onStateChanged(e, state) {
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
var player = this;
|
|
|
|
updatePlayerState(player, state, 'statechange');
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function onPlaybackStart(e, state) {
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
var player = this;
|
|
|
|
|
|
|
|
updatePlayerState(player, state, e.type);
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function onPlaybackStopped(e, state) {
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
var player = this;
|
|
|
|
|
|
|
|
hideMediaControls();
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function releaseCurrentPlayer() {
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
if (currentPlayer) {
|
|
|
|
|
|
|
|
events.off(currentPlayer, 'playbackstart', onPlaybackStart);
|
|
|
|
events.off(currentPlayer, 'playbackstop', onPlaybackStopped);
|
|
|
|
events.off(currentPlayer, 'unpause', onGeneralEvent);
|
|
|
|
events.off(currentPlayer, 'pause', onGeneralEvent);
|
|
|
|
events.off(currentPlayer, 'statechange', onStateChanged);
|
|
|
|
events.off(currentPlayer, 'timeupdate', onGeneralEvent);
|
|
|
|
|
|
|
|
currentPlayer = null;
|
|
|
|
|
|
|
|
hideMediaControls();
|
|
|
|
}
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function hideMediaControls() {
|
2019-03-17 00:31:56 +00:00
|
|
|
lastUpdateTime = 0;
|
|
|
|
|
|
|
|
if (navigator.mediaSession) {
|
|
|
|
navigator.mediaSession.metadata = null;
|
|
|
|
} else {
|
|
|
|
window.NativeShell.hideMediaSession();
|
|
|
|
}
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function bindToPlayer(player) {
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
releaseCurrentPlayer();
|
|
|
|
|
|
|
|
if (!player) {
|
|
|
|
return;
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
currentPlayer = player;
|
|
|
|
|
|
|
|
var state = playbackManager.getPlayerState(player);
|
|
|
|
updatePlayerState(player, state, 'init');
|
|
|
|
|
|
|
|
events.on(currentPlayer, 'playbackstart', onPlaybackStart);
|
|
|
|
events.on(currentPlayer, 'playbackstop', onPlaybackStopped);
|
|
|
|
events.on(currentPlayer, 'unpause', onGeneralEvent);
|
|
|
|
events.on(currentPlayer, 'pause', onGeneralEvent);
|
|
|
|
events.on(currentPlayer, 'statechange', onStateChanged);
|
|
|
|
events.on(currentPlayer, 'timeupdate', onGeneralEvent);
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
|
2019-05-05 23:55:42 +02:00
|
|
|
function execute(name) {
|
|
|
|
playbackManager[name](currentPlayer);
|
|
|
|
}
|
2019-03-17 00:31:56 +00:00
|
|
|
if (navigator.mediaSession) {
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2019-03-17 00:31:56 +00:00
|
|
|
navigator.mediaSession.setActionHandler('previoustrack', function () {
|
|
|
|
execute('previousTrack');
|
|
|
|
});
|
2019-03-21 21:54:36 +00:00
|
|
|
|
2019-03-17 00:31:56 +00:00
|
|
|
navigator.mediaSession.setActionHandler('nexttrack', function () {
|
|
|
|
execute('nextTrack');
|
|
|
|
});
|
2019-03-21 21:54:36 +00:00
|
|
|
|
2019-03-17 00:31:56 +00:00
|
|
|
navigator.mediaSession.setActionHandler('play', function () {
|
|
|
|
execute('unpause');
|
|
|
|
});
|
2019-03-21 21:54:36 +00:00
|
|
|
|
2019-03-17 00:31:56 +00:00
|
|
|
navigator.mediaSession.setActionHandler('pause', function () {
|
|
|
|
execute('pause');
|
|
|
|
});
|
2019-03-21 21:54:36 +00:00
|
|
|
|
2019-03-17 00:31:56 +00:00
|
|
|
navigator.mediaSession.setActionHandler('seekbackward', function () {
|
|
|
|
execute('rewind');
|
|
|
|
});
|
2019-03-21 21:54:36 +00:00
|
|
|
|
2019-03-17 00:31:56 +00:00
|
|
|
navigator.mediaSession.setActionHandler('seekforward', function () {
|
|
|
|
execute('fastForward');
|
|
|
|
});
|
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
events.on(playbackManager, 'playerchange', function () {
|
|
|
|
|
|
|
|
bindToPlayer(playbackManager.getCurrentPlayer());
|
|
|
|
});
|
|
|
|
|
|
|
|
bindToPlayer(playbackManager.getCurrentPlayer());
|
2019-05-05 23:55:42 +02:00
|
|
|
});
|