feat: add media session (wip)

This commit is contained in:
MAZE 2024-05-11 16:26:25 +04:30
parent df1b05f7ce
commit f311ec114e
2 changed files with 14 additions and 4 deletions

View file

@ -105,6 +105,8 @@ export function App() {
<audio
aria-hidden={true}
loop
muted
ref={audio}
src="/sounds/2-seconds-of-silence.mp3"
/>

View file

@ -8,8 +8,8 @@ export function useMediaSession() {
const isPlaying = useSoundStore(state => state.isPlaying);
useEffect(() => {
if (isPlaying) {
ref.current?.play().then(() => {
if (ref.current) {
ref.current.addEventListener('play', () => {
console.log('hi');
navigator.mediaSession.metadata = new MediaMetadata({
title: 'Moodist',
@ -17,10 +17,18 @@ export function useMediaSession() {
navigator.mediaSession.playbackState = 'playing';
});
ref.current.addEventListener('pause', () => {
navigator.mediaSession.playbackState = 'paused';
});
}
}, []);
useEffect(() => {
if (isPlaying) {
ref.current?.play();
} else {
ref.current?.pause();
navigator.mediaSession.playbackState = 'paused';
}
}, [isPlaying]);