mirror of
https://github.com/darksaid98/new-phone-who-dis.git
synced 2026-12-03 22:16:54 +00:00
Remove dependency comments and use useRef instead for useSound hook (#117)
* Remove dependency comments and use useRef instead for useSound hook * Handle change URL volume and loop gracefully
This commit is contained in:
1 parent
54bdd07e9c
commit
9cfc3fa8ec
2 files changed
+52
-36
No files matched your search
@@ -1,4 +1,4 @@
|
||||
import React from 'react';
|
||||
import React, { useState } from 'react';
|
||||
|
||||
import { AppWrapper } from '../../../ui/components';
|
||||
import { AppContent } from '../../../ui/components/AppContent';
|
||||
@@ -6,12 +6,17 @@ import { useApp } from '../../../os/apps/hooks/useApps';
|
||||
import { useExample } from '../hooks/useExample';
|
||||
import { ExampleThemeProvider } from '../providers/ExampleThemeProvider';
|
||||
import { AppTitle } from '../../../ui/components/AppTitle';
|
||||
import { Button } from '@material-ui/core';
|
||||
import PlayIcon from '@material-ui/icons/PlayArrow';
|
||||
import StopIcon from '@material-ui/icons/Stop';
|
||||
import { Button, IconButton } from '@material-ui/core';
|
||||
import useSound from '../../../os/phone/hooks/useSound';
|
||||
|
||||
export const ExampleApp = () => {
|
||||
// calling the example hook, and we assign the value to a variable
|
||||
const exampleString = useExample();
|
||||
const example = useApp('EXAMPLE');
|
||||
const [soundName, setSound] = useState('pixel.ogg');
|
||||
const { play, playing, stop } = useSound('media/ringtones/' + soundName);
|
||||
return (
|
||||
<ExampleThemeProvider>
|
||||
<AppWrapper>
|
||||
@@ -21,6 +26,12 @@ export const ExampleApp = () => {
|
||||
<Button color='primary'>{example.id}</Button>
|
||||
{/* Here we are using the value in a h3 tag */}
|
||||
<h3>{exampleString}</h3>
|
||||
<IconButton onClick={() => (playing ? stop() : play())}>
|
||||
{playing ? <StopIcon /> : <PlayIcon />}
|
||||
</IconButton>
|
||||
<Button onClick={() => setSound('marimba.ogg')}>
|
||||
Change sound on the fly
|
||||
</Button>
|
||||
</AppContent>
|
||||
</AppWrapper>
|
||||
</ExampleThemeProvider>
|
||||
|
||||
@@ -13,70 +13,75 @@ interface ISoundOptions {
|
||||
*
|
||||
* @param { volume, interrupt, loop} Additional options
|
||||
**/
|
||||
|
||||
const useSound = (
|
||||
url: string,
|
||||
{ volume = 1, interrupt = false, loop = false }: ISoundOptions = {}
|
||||
) => {
|
||||
const isMounted = useRef<boolean>(false);
|
||||
const [isMounted, setMounted] = useState<boolean>(false);
|
||||
const soundRef = useRef<Howl>(null);
|
||||
|
||||
const [playing, setPlaying] = useState<boolean>(false);
|
||||
|
||||
const [sound, setSound] = useState<Howl | null>(null);
|
||||
|
||||
// Mount & Dismount handling
|
||||
useEffect(() => {
|
||||
isMounted.current = true;
|
||||
const sound = new Howl({
|
||||
soundRef.current?.unload();
|
||||
setPlaying(false);
|
||||
setMounted(false);
|
||||
|
||||
soundRef.current = new Howl({
|
||||
src: url,
|
||||
volume,
|
||||
loop,
|
||||
volume: 1,
|
||||
loop: false,
|
||||
});
|
||||
setSound(sound);
|
||||
|
||||
setMounted(true);
|
||||
|
||||
return () => {
|
||||
isMounted.current = false;
|
||||
soundRef.current?.unload();
|
||||
soundRef.current = undefined;
|
||||
};
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, []);
|
||||
}, [url]);
|
||||
|
||||
useEffect(() => {
|
||||
if (sound) {
|
||||
sound.volume(volume);
|
||||
if (isMounted) {
|
||||
soundRef.current.volume(volume);
|
||||
}
|
||||
// Shouldnt need to rerun if sound changes
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [volume]);
|
||||
}, [volume, isMounted]);
|
||||
|
||||
useEffect(() => {
|
||||
if (isMounted) {
|
||||
soundRef.current.loop(loop);
|
||||
}
|
||||
}, [loop, isMounted]);
|
||||
|
||||
const play = useCallback(() => {
|
||||
if (!sound) return;
|
||||
if (!soundRef.current) return;
|
||||
|
||||
if (interrupt) return sound.stop();
|
||||
|
||||
sound.play();
|
||||
|
||||
if (isMounted.current) {
|
||||
sound.once('end', () => {
|
||||
if (!sound.playing()) {
|
||||
if (isMounted) {
|
||||
soundRef.current.play();
|
||||
soundRef.current.once('end', () => {
|
||||
if (!soundRef.current.playing()) {
|
||||
setPlaying(false);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
if (isMounted.current) {
|
||||
setPlaying(true);
|
||||
}
|
||||
}, [interrupt, sound]);
|
||||
}, [isMounted]);
|
||||
|
||||
const stop = useCallback(() => {
|
||||
if (!sound) return;
|
||||
if (!soundRef.current) return;
|
||||
|
||||
sound.stop();
|
||||
|
||||
if (isMounted.current) {
|
||||
if (isMounted) {
|
||||
soundRef.current.stop();
|
||||
setPlaying(false);
|
||||
}
|
||||
}, [sound]);
|
||||
}, [isMounted]);
|
||||
|
||||
useEffect(() => {
|
||||
if (interrupt && isMounted) {
|
||||
stop();
|
||||
}
|
||||
}, [interrupt, isMounted, stop]);
|
||||
|
||||
return { play, playing, stop };
|
||||
};
|
||||
|
||||
Reference in new issue
Block a user