Skip to content

Commit

Permalink
Merge pull request #6244 from thornbill/fix-playing-parts
Browse files Browse the repository at this point in the history
  • Loading branch information
thornbill authored Oct 24, 2024
2 parents a9586d4 + 571b280 commit de87916
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 32 deletions.
7 changes: 5 additions & 2 deletions src/apps/experimental/components/library/PlayAllButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,21 @@ const PlayAllButton: FC<PlayAllButtonProps> = ({ item, items, viewType, hasFilte
SortBy: [libraryViewSettings.SortBy],
SortOrder: [libraryViewSettings.SortOrder]
}
}).catch(err => {
console.error('[PlayAllButton] failed to play', err);
});
} else {
playbackManager.play({
items: items,
items,
autoplay: true,
queryOptions: {
ParentId: item?.Id ?? undefined,
...getFiltersQuery(viewType, libraryViewSettings),
SortBy: [libraryViewSettings.SortBy],
SortOrder: [libraryViewSettings.SortOrder]
}

}).catch(err => {
console.error('[PlayAllButton] failed to play', err);
});
}
}, [hasFilters, item, items, libraryViewSettings, viewType]);
Expand Down
6 changes: 5 additions & 1 deletion src/apps/experimental/components/library/QueueButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@ const QueueButton: FC<QueueButtonProps> = ({ item, items, hasFilters }) => {
if (item && !hasFilters) {
playbackManager.queue({
items: [item]
}).catch(err => {
console.error('[QueueButton] failed to add to queue', err);
});
} else {
playbackManager.queue({
items: items
items
}).catch(err => {
console.error('[QueueButton] failed to add to queue', err);
});
}
}, [hasFilters, item, items]);
Expand Down
4 changes: 3 additions & 1 deletion src/apps/experimental/components/library/ShuffleButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@ const ShuffleButton: FC<ShuffleButtonProps> = ({ item, items, viewType, hasFilte
playbackManager.shuffle(item);
} else {
playbackManager.play({
items: items,
items,
autoplay: true,
queryOptions: {
ParentId: item?.Id ?? undefined,
...getFiltersQuery(viewType, libraryViewSettings),
SortBy: [ItemSortBy.Random]
}
}).catch(err => {
console.error('[ShuffleButton] failed to play', err);
});
}
}, [hasFilters, item, items, libraryViewSettings, viewType]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ function playAllFromHere(opts: PlayAllFromHereOptions) {
}

if (!ids.length) {
return;
return Promise.resolve();
}

if (queue) {
Expand Down Expand Up @@ -168,13 +168,17 @@ const MoreCommandsButton: FC<MoreCommandsButtonProps> = ({
item: item || {},
items: items || [],
serverId: item?.ServerId
}).catch(err => {
console.error('[MoreCommandsButton] failed to play', err);
});
} else if (result.command === 'queueallfromhere') {
playAllFromHere({
item: item || {},
items: items || [],
serverId: item?.ServerId,
queue: true
}).catch(err => {
console.error('[MoreCommandsButton] failed to play', err);
});
} else if (result.deleted) {
if (result?.itemId !== itemId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,17 @@ const PlayOrResumeButton: FC<PlayOrResumeButtonProps> = ({
);
playbackManager.play({
items: [channel]
}).catch(err => {
console.error('[PlayOrResumeButton] failed to play', err);
});
return;
}

playbackManager.play({
items: [item],
...playOptions
}).catch(err => {
console.error('[PlayOrResumeButton] failed to play', err);
});
}, [apiContext, item, playOptions, queryClient]);

Expand Down
62 changes: 35 additions & 27 deletions src/components/playback/playbackmanager.js
Original file line number Diff line number Diff line change
Expand Up @@ -2039,12 +2039,12 @@ export class PlaybackManager {
self.translateItemsForPlayback = translateItemsForPlayback;
self.getItemsForPlayback = getItemsForPlayback;

self.play = function (options) {
self.play = async function (options) {
normalizePlayOptions(options);

if (self._currentPlayer) {
if (options.enableRemotePlayers === false && !self._currentPlayer.isLocalPlayer) {
return Promise.reject();
throw new Error('Remote players are disabled');
}

if (!self._currentPlayer.isLocalPlayer) {
Expand All @@ -2056,29 +2056,35 @@ export class PlaybackManager {
loading.show();
}

if (options.items) {
return translateItemsForPlayback(options.items, options)
.then((items) => getAdditionalParts(items))
.then(function (allItems) {
const flattened = allItems.flatMap(i => i);
return playWithIntros(flattened, options);
});
} else {
let { items } = options;
// If items were not passed directly, fetch them by ID
if (!items) {
if (!options.serverId) {
throw new Error('serverId required!');
}

return getItemsForPlayback(options.serverId, {
items = (await getItemsForPlayback(options.serverId, {
Ids: options.ids.join(',')
}).then(function (result) {
return translateItemsForPlayback(result.Items, options)
.then((items) => getAdditionalParts(items))
.then(function (allItems) {
const flattened = allItems.flatMap(i => i);
return playWithIntros(flattened, options);
});
});
})).Items;
}

// Prepare the list of items
items = await translateItemsForPlayback(items, options);
// Add any additional parts for movies or episodes
items = await getAdditionalParts(items);
// Adjust the start index for additional parts added to the queue
if (options.startIndex) {
let adjustedStartIndex = 0;
for (let i = 0; i < options.startIndex; i++) {
adjustedStartIndex += items[i].length;
}

options.startIndex = adjustedStartIndex;
}
// getAdditionalParts returns an array of arrays of items, so flatten it
items = items.flat();

return playWithIntros(items, options);
};

function getPlayerData(player) {
Expand Down Expand Up @@ -2217,20 +2223,22 @@ export class PlaybackManager {
}

const getAdditionalParts = async (items) => {
const getOneAdditionalPart = async function (item) {
let retVal = [item];
if (item.PartCount && item.PartCount > 1 && (item.Type === 'Movie' || item.Type === 'Episode')) {
const getItemAndParts = async function (item) {
if (
item.PartCount && item.PartCount > 1
&& [ BaseItemKind.Episode, BaseItemKind.Movie ].includes(item.Type)
) {
const client = ServerConnections.getApiClient(item.ServerId);
const user = await client.getCurrentUser();
const additionalParts = await client.getAdditionalVideoParts(user.Id, item.Id);
if (additionalParts.Items.length) {
retVal = [item, ...additionalParts.Items];
return [ item, ...additionalParts.Items ];
}
}
return retVal;
return [ item ];
};

return Promise.all(items.flatMap(async (item) => getOneAdditionalPart(item)));
return Promise.all(items.map(getItemAndParts));
};

function playWithIntros(items, options) {
Expand Down Expand Up @@ -3105,11 +3113,11 @@ export class PlaybackManager {
};

self.queue = function (options, player = this._currentPlayer) {
queue(options, '', player);
return queue(options, '', player);
};

self.queueNext = function (options, player = this._currentPlayer) {
queue(options, 'next', player);
return queue(options, 'next', player);
};

function queue(options, mode, player) {
Expand Down
2 changes: 2 additions & 0 deletions src/components/playlisteditor/playlisteditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ function addToPlaylist(dlg: DialogElement, id: string) {
playbackManager.queue({
serverId: currentServerId,
ids: itemIds.split(',')
}).catch(err => {
console.error('[PlaylistEditor] failed to add to queue', err);
});
dlg.submitted = true;
dialogHelper.close(dlg);
Expand Down

0 comments on commit de87916

Please sign in to comment.