mirror of
https://github.com/remvze/moodist.git
synced 2025-12-17 08:54:13 +00:00
feat: add media session (wip)
This commit is contained in:
parent
9ad16306cf
commit
1547b0a436
1 changed files with 43 additions and 13 deletions
|
|
@ -1,4 +1,4 @@
|
||||||
import { useMemo, useEffect } from 'react';
|
import { useMemo, useEffect, useRef } from 'react';
|
||||||
import { useShallow } from 'zustand/react/shallow';
|
import { useShallow } from 'zustand/react/shallow';
|
||||||
import { BiSolidHeart } from 'react-icons/bi/index';
|
import { BiSolidHeart } from 'react-icons/bi/index';
|
||||||
import { Howler } from 'howler';
|
import { Howler } from 'howler';
|
||||||
|
|
@ -85,7 +85,34 @@ export function App() {
|
||||||
return [...favorites, ...categories];
|
return [...favorites, ...categories];
|
||||||
}, [favoriteSounds, 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 (
|
return (
|
||||||
|
<>
|
||||||
<SnackbarProvider>
|
<SnackbarProvider>
|
||||||
<StoreConsumer>
|
<StoreConsumer>
|
||||||
<Container>
|
<Container>
|
||||||
|
|
@ -98,5 +125,8 @@ export function App() {
|
||||||
<SharedModal />
|
<SharedModal />
|
||||||
</StoreConsumer>
|
</StoreConsumer>
|
||||||
</SnackbarProvider>
|
</SnackbarProvider>
|
||||||
|
|
||||||
|
<audio aria-hidden={true} muted ref={audioElement} src="" />
|
||||||
|
</>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue