From 1547b0a436bd9a77c19fc5d37be3cb3e123e6117 Mon Sep 17 00:00:00 2001 From: MAZE Date: Sat, 11 May 2024 16:20:48 +0330 Subject: [PATCH] feat: add media session (wip) --- src/components/app/app.tsx | 56 +++++++++++++++++++++++++++++--------- 1 file changed, 43 insertions(+), 13 deletions(-) diff --git a/src/components/app/app.tsx b/src/components/app/app.tsx index 713e24a..5772bf2 100644 --- a/src/components/app/app.tsx +++ b/src/components/app/app.tsx @@ -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'; @@ -85,18 +85,48 @@ export function App() { return [...favorites, ...categories]; }, [favoriteSounds, categories]); - return ( - - - -
- - - + const audioElement = useRef(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 ( + <> + + + +
+ + + + + + + + + +