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 <audio
aria-hidden={true} aria-hidden={true}
loop
muted
ref={audio} ref={audio}
src="/sounds/2-seconds-of-silence.mp3" src="/sounds/2-seconds-of-silence.mp3"
/> />

View file

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