mirror of
https://github.com/Stremio/stremio-web.git
synced 2026-04-21 11:42:05 +00:00
feat(Player): add media session support
This commit is contained in:
parent
1e241c7926
commit
c26dac2154
1 changed files with 47 additions and 0 deletions
|
|
@ -532,6 +532,53 @@ const Player = ({ urlParams, queryParams }) => {
|
||||||
}
|
}
|
||||||
}, [settings.pauseOnMinimize, shell.windowClosed, shell.windowHidden]);
|
}, [settings.pauseOnMinimize, shell.windowClosed, shell.windowHidden]);
|
||||||
|
|
||||||
|
// Media Session PlaybackState
|
||||||
|
React.useEffect(() => {
|
||||||
|
if (!navigator.mediaSession) return;
|
||||||
|
|
||||||
|
const playbackState = !video.state.paused ? 'playing' : 'paused';
|
||||||
|
navigator.mediaSession.playbackState = playbackState;
|
||||||
|
|
||||||
|
return () => navigator.mediaSession.playbackState = 'none';
|
||||||
|
}, [video.state.paused]);
|
||||||
|
|
||||||
|
// Media Session Metadata
|
||||||
|
React.useEffect(() => {
|
||||||
|
if (!navigator.mediaSession) return;
|
||||||
|
|
||||||
|
const metaItem = player.metaItem && player.metaItem.type === 'Ready' ? player.metaItem.content : null;
|
||||||
|
const videoId = player.selected ? player.selected.streamRequest.path.id : null;
|
||||||
|
const video = metaItem ? metaItem.videos.find(({ id }) => id === videoId) : null;
|
||||||
|
|
||||||
|
const videoInfo = video && video.season && video.episode ? ` (${video.season}x${video.episode})`: null;
|
||||||
|
const videoTitle = video ? `${video.title}${videoInfo}` : null;
|
||||||
|
const metaTitle = metaItem ? metaItem.name : null;
|
||||||
|
const imageUrl = metaItem ? metaItem.poster : null;
|
||||||
|
|
||||||
|
const title = videoTitle ?? metaTitle;
|
||||||
|
const artist = videoTitle ? metaTitle : undefined;
|
||||||
|
const artwork = imageUrl ? [{ src: imageUrl }] : undefined;
|
||||||
|
|
||||||
|
if (title) {
|
||||||
|
navigator.mediaSession.metadata = new MediaMetadata({
|
||||||
|
title,
|
||||||
|
artist,
|
||||||
|
artwork,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}, [player.metaItem, player.selected]);
|
||||||
|
|
||||||
|
// Media Session Actions
|
||||||
|
React.useEffect(() => {
|
||||||
|
if (!navigator.mediaSession) return;
|
||||||
|
|
||||||
|
navigator.mediaSession.setActionHandler('play', onPlayRequested);
|
||||||
|
navigator.mediaSession.setActionHandler('pause', onPauseRequested);
|
||||||
|
|
||||||
|
const nexVideoCallback = player.nextVideo ? onNextVideoRequested : null;
|
||||||
|
navigator.mediaSession.setActionHandler('nexttrack', nexVideoCallback);
|
||||||
|
}, [player.nextVideo, onPlayRequested, onPauseRequested, onNextVideoRequested]);
|
||||||
|
|
||||||
React.useLayoutEffect(() => {
|
React.useLayoutEffect(() => {
|
||||||
const onKeyDown = (event) => {
|
const onKeyDown = (event) => {
|
||||||
switch (event.code) {
|
switch (event.code) {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue