mirror of
https://github.com/remvze/moodist.git
synced 2025-10-19 04:50:36 -04:00
feat: add media session (wip)
This commit is contained in:
parent
ea0dfff9c1
commit
df1b05f7ce
BIN
public/sounds/2-seconds-of-silence.mp3
Normal file
BIN
public/sounds/2-seconds-of-silence.mp3
Normal file
Binary file not shown.
@ -86,9 +86,10 @@ export function App() {
|
|||||||
return [...favorites, ...categories];
|
return [...favorites, ...categories];
|
||||||
}, [favoriteSounds, categories]);
|
}, [favoriteSounds, categories]);
|
||||||
|
|
||||||
useMediaSession();
|
const audio = useMediaSession();
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
<>
|
||||||
<SnackbarProvider>
|
<SnackbarProvider>
|
||||||
<StoreConsumer>
|
<StoreConsumer>
|
||||||
<Container>
|
<Container>
|
||||||
@ -101,5 +102,12 @@ export function App() {
|
|||||||
<SharedModal />
|
<SharedModal />
|
||||||
</StoreConsumer>
|
</StoreConsumer>
|
||||||
</SnackbarProvider>
|
</SnackbarProvider>
|
||||||
|
|
||||||
|
<audio
|
||||||
|
aria-hidden={true}
|
||||||
|
ref={audio}
|
||||||
|
src="/sounds/2-seconds-of-silence.mp3"
|
||||||
|
/>
|
||||||
|
</>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -1,31 +1,28 @@
|
|||||||
import { useEffect } from 'react';
|
import { useEffect, useRef } from 'react';
|
||||||
|
|
||||||
import { useSoundStore } from '@/store';
|
import { useSoundStore } from '@/store';
|
||||||
|
|
||||||
export function useMediaSession() {
|
export function useMediaSession() {
|
||||||
const play = useSoundStore(state => state.play);
|
const ref = useRef<HTMLAudioElement | null>(null);
|
||||||
const pause = useSoundStore(state => state.pause);
|
|
||||||
const isPlaying = useSoundStore(state => state.isPlaying);
|
const isPlaying = useSoundStore(state => state.isPlaying);
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
try {
|
|
||||||
navigator.mediaSession.setActionHandler('play', play);
|
|
||||||
navigator.mediaSession.setActionHandler('pause', pause);
|
|
||||||
navigator.mediaSession.setActionHandler('stop', pause);
|
|
||||||
} catch (error) {
|
|
||||||
console.log('Media session is no supported yet');
|
|
||||||
}
|
|
||||||
}, [play, pause]);
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (isPlaying) {
|
if (isPlaying) {
|
||||||
|
ref.current?.play().then(() => {
|
||||||
|
console.log('hi');
|
||||||
navigator.mediaSession.metadata = new MediaMetadata({
|
navigator.mediaSession.metadata = new MediaMetadata({
|
||||||
title: 'Moodist',
|
title: 'Moodist',
|
||||||
});
|
});
|
||||||
|
|
||||||
navigator.mediaSession.playbackState = 'playing';
|
navigator.mediaSession.playbackState = 'playing';
|
||||||
|
});
|
||||||
} else {
|
} else {
|
||||||
|
ref.current?.pause();
|
||||||
|
|
||||||
navigator.mediaSession.playbackState = 'paused';
|
navigator.mediaSession.playbackState = 'paused';
|
||||||
}
|
}
|
||||||
}, [isPlaying]);
|
}, [isPlaying]);
|
||||||
|
|
||||||
|
return ref;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user