Skip to content

Commit

Permalink
feat: add media session (wip)
Browse files Browse the repository at this point in the history
  • Loading branch information
remvze committed May 11, 2024
1 parent c66cddc commit 11a4514
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/components/app/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { FADE_OUT } from '@/constants/events';

import type { Sound } from '@/data/types';
import { subscribe } from '@/lib/event';
import { useMediaSession } from '@/hooks/use-media-session';

export function App() {
const categories = useMemo(() => sounds.categories, []);
Expand Down Expand Up @@ -85,6 +86,8 @@ export function App() {
return [...favorites, ...categories];
}, [favoriteSounds, categories]);

useMediaSession();

return (
<SnackbarProvider>
<StoreConsumer>
Expand Down
19 changes: 19 additions & 0 deletions src/hooks/use-media-session.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { useEffect } from 'react';

import { useSoundStore } from '@/store';

export function useMediaSession() {
const isPlaying = useSoundStore(state => state.isPlaying);

useEffect(() => {
if ('mediaSession' in navigator) {
if (isPlaying) {
navigator.mediaSession.metadata = new MediaMetadata({
title: 'Moodist - Ambient Sounds',
});
} else {
navigator.mediaSession.metadata = null;
}
}
}, [isPlaying]);
}

0 comments on commit 11a4514

Please sign in to comment.