Skip to content

Commit

Permalink
fix: add media session
Browse files Browse the repository at this point in the history
  • Loading branch information
remvze committed Jan 28, 2024
1 parent 463667c commit 81f33d9
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion src/components/app/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ export function App() {
const categories = useMemo(() => sounds.categories, []);

const favorites = useSoundStore(useShallow(state => state.getFavorites()));
const play = useSoundStore(state => state.play);
const pause = useSoundStore(state => state.pause);
const isPlaying = useSoundStore(state => state.isPlaying);

const favoriteSounds = useMemo(() => {
const favoriteSounds = categories
Expand Down Expand Up @@ -60,10 +63,33 @@ export function App() {
const { ctx } = Howler;
const dest = ctx.createMediaStreamDestination();
audio.current.srcObject = dest.stream;
audio.current.play();
}
}, []);

useEffect(() => {
try {
navigator.mediaSession.setActionHandler('play', play);
navigator.mediaSession.setActionHandler('pause', pause);
navigator.mediaSession.setActionHandler('stop', pause);
} catch (error) {
console.log('Media session is no supported yet');
}
}, [play, pause]);

useEffect(() => {
if (isPlaying) {
navigator.mediaSession.metadata = new MediaMetadata({
title: 'Moodist',
});

audio.current?.play();
navigator.mediaSession.playbackState = 'playing';
} else {
audio.current?.pause();
navigator.mediaSession.playbackState = 'paused';
}
}, [isPlaying]);

const allCategories = useMemo(() => {
const favorites = [];

Expand Down

0 comments on commit 81f33d9

Please sign in to comment.