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 9ad1630 commit 1547b0a
Showing 1 changed file with 43 additions and 13 deletions.
56 changes: 43 additions & 13 deletions src/components/app/app.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useMemo, useEffect } from 'react';
import { useMemo, useEffect, useRef } from 'react';
import { useShallow } from 'zustand/react/shallow';
import { BiSolidHeart } from 'react-icons/bi/index';
import { Howler } from 'howler';
Expand Down Expand Up @@ -85,18 +85,48 @@ export function App() {
return [...favorites, ...categories];
}, [favoriteSounds, categories]);

const audioElement = useRef<HTMLAudioElement | null>(null);
const isPlaying = useSoundStore(state => state.isPlaying);

useEffect(() => {
const dest = Howler.ctx.createMediaStreamDestination();

if (audioElement.current) {
audioElement.current.srcObject = dest.stream;
}
}, []);

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

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

return (
<SnackbarProvider>
<StoreConsumer>
<Container>
<div id="app" />
<Buttons />
<Categories categories={allCategories} />
</Container>

<Toolbar />
<SharedModal />
</StoreConsumer>
</SnackbarProvider>
<>
<SnackbarProvider>
<StoreConsumer>
<Container>
<div id="app" />
<Buttons />
<Categories categories={allCategories} />
</Container>

<Toolbar />
<SharedModal />
</StoreConsumer>
</SnackbarProvider>

<audio aria-hidden={true} muted ref={audioElement} src="" />
</>
);
}

0 comments on commit 1547b0a

Please sign in to comment.