mirror of
https://github.com/remvze/moodist.git
synced 2026-02-28 00:53:13 +08:00
feat: add media session (wip)
This commit is contained in:
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,20 +86,28 @@ export function App() {
|
|||||||
return [...favorites, ...categories];
|
return [...favorites, ...categories];
|
||||||
}, [favoriteSounds, categories]);
|
}, [favoriteSounds, categories]);
|
||||||
|
|
||||||
useMediaSession();
|
const audio = useMediaSession();
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<SnackbarProvider>
|
<>
|
||||||
<StoreConsumer>
|
<SnackbarProvider>
|
||||||
<Container>
|
<StoreConsumer>
|
||||||
<div id="app" />
|
<Container>
|
||||||
<Buttons />
|
<div id="app" />
|
||||||
<Categories categories={allCategories} />
|
<Buttons />
|
||||||
</Container>
|
<Categories categories={allCategories} />
|
||||||
|
</Container>
|
||||||
|
|
||||||
<Toolbar />
|
<Toolbar />
|
||||||
<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) {
|
||||||
navigator.mediaSession.metadata = new MediaMetadata({
|
ref.current?.play().then(() => {
|
||||||
title: 'Moodist',
|
console.log('hi');
|
||||||
});
|
navigator.mediaSession.metadata = new MediaMetadata({
|
||||||
|
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;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user