mirror of
https://github.com/darksaid98/new-phone-who-dis.git
synced 2026-12-04 22:16:40 +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 { AppWrapper } from '../../../ui/components';
|
||||||
import { AppContent } from '../../../ui/components/AppContent';
|
import { AppContent } from '../../../ui/components/AppContent';
|
||||||
@@ -6,12 +6,17 @@ import { useApp } from '../../../os/apps/hooks/useApps';
|
|||||||
import { useExample } from '../hooks/useExample';
|
import { useExample } from '../hooks/useExample';
|
||||||
import { ExampleThemeProvider } from '../providers/ExampleThemeProvider';
|
import { ExampleThemeProvider } from '../providers/ExampleThemeProvider';
|
||||||
import { AppTitle } from '../../../ui/components/AppTitle';
|
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 = () => {
|
export const ExampleApp = () => {
|
||||||
// calling the example hook, and we assign the value to a variable
|
// calling the example hook, and we assign the value to a variable
|
||||||
const exampleString = useExample();
|
const exampleString = useExample();
|
||||||
const example = useApp('EXAMPLE');
|
const example = useApp('EXAMPLE');
|
||||||
|
const [soundName, setSound] = useState('pixel.ogg');
|
||||||
|
const { play, playing, stop } = useSound('media/ringtones/' + soundName);
|
||||||
return (
|
return (
|
||||||
<ExampleThemeProvider>
|
<ExampleThemeProvider>
|
||||||
<AppWrapper>
|
<AppWrapper>
|
||||||
@@ -21,6 +26,12 @@ export const ExampleApp = () => {
|
|||||||
<Button color='primary'>{example.id}</Button>
|
<Button color='primary'>{example.id}</Button>
|
||||||
{/* Here we are using the value in a h3 tag */}
|
{/* Here we are using the value in a h3 tag */}
|
||||||
<h3>{exampleString}</h3>
|
<h3>{exampleString}</h3>
|
||||||
|
<IconButton onClick={() => (playing ? stop() : play())}>
|
||||||
|
{playing ? <StopIcon /> : <PlayIcon />}
|
||||||
|
</IconButton>
|
||||||
|
<Button onClick={() => setSound('marimba.ogg')}>
|
||||||
|
Change sound on the fly
|
||||||
|
</Button>
|
||||||
</AppContent>
|
</AppContent>
|
||||||
</AppWrapper>
|
</AppWrapper>
|
||||||
</ExampleThemeProvider>
|
</ExampleThemeProvider>
|
||||||
|
|||||||
@@ -13,70 +13,75 @@ interface ISoundOptions {
|
|||||||
*
|
*
|
||||||
* @param { volume, interrupt, loop} Additional options
|
* @param { volume, interrupt, loop} Additional options
|
||||||
**/
|
**/
|
||||||
|
|
||||||
const useSound = (
|
const useSound = (
|
||||||
url: string,
|
url: string,
|
||||||
{ volume = 1, interrupt = false, loop = false }: ISoundOptions = {}
|
{ 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 [playing, setPlaying] = useState<boolean>(false);
|
||||||
|
|
||||||
const [sound, setSound] = useState<Howl | null>(null);
|
|
||||||
|
|
||||||
// Mount & Dismount handling
|
// Mount & Dismount handling
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
isMounted.current = true;
|
soundRef.current?.unload();
|
||||||
const sound = new Howl({
|
setPlaying(false);
|
||||||
|
setMounted(false);
|
||||||
|
|
||||||
|
soundRef.current = new Howl({
|
||||||
src: url,
|
src: url,
|
||||||
volume,
|
volume: 1,
|
||||||
loop,
|
loop: false,
|
||||||
});
|
});
|
||||||
setSound(sound);
|
|
||||||
|
setMounted(true);
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
isMounted.current = false;
|
soundRef.current?.unload();
|
||||||
|
soundRef.current = undefined;
|
||||||
};
|
};
|
||||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
}, [url]);
|
||||||
}, []);
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (sound) {
|
if (isMounted) {
|
||||||
sound.volume(volume);
|
soundRef.current.volume(volume);
|
||||||
}
|
}
|
||||||
// Shouldnt need to rerun if sound changes
|
}, [volume, isMounted]);
|
||||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
||||||
}, [volume]);
|
useEffect(() => {
|
||||||
|
if (isMounted) {
|
||||||
|
soundRef.current.loop(loop);
|
||||||
|
}
|
||||||
|
}, [loop, isMounted]);
|
||||||
|
|
||||||
const play = useCallback(() => {
|
const play = useCallback(() => {
|
||||||
if (!sound) return;
|
if (!soundRef.current) return;
|
||||||
|
|
||||||
if (interrupt) return sound.stop();
|
if (isMounted) {
|
||||||
|
soundRef.current.play();
|
||||||
sound.play();
|
soundRef.current.once('end', () => {
|
||||||
|
if (!soundRef.current.playing()) {
|
||||||
if (isMounted.current) {
|
|
||||||
sound.once('end', () => {
|
|
||||||
if (!sound.playing()) {
|
|
||||||
setPlaying(false);
|
setPlaying(false);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
|
||||||
|
|
||||||
if (isMounted.current) {
|
|
||||||
setPlaying(true);
|
setPlaying(true);
|
||||||
}
|
}
|
||||||
}, [interrupt, sound]);
|
}, [isMounted]);
|
||||||
|
|
||||||
const stop = useCallback(() => {
|
const stop = useCallback(() => {
|
||||||
if (!sound) return;
|
if (!soundRef.current) return;
|
||||||
|
|
||||||
sound.stop();
|
if (isMounted) {
|
||||||
|
soundRef.current.stop();
|
||||||
if (isMounted.current) {
|
|
||||||
setPlaying(false);
|
setPlaying(false);
|
||||||
}
|
}
|
||||||
}, [sound]);
|
}, [isMounted]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (interrupt && isMounted) {
|
||||||
|
stop();
|
||||||
|
}
|
||||||
|
}, [interrupt, isMounted, stop]);
|
||||||
|
|
||||||
return { play, playing, stop };
|
return { play, playing, stop };
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in new issue
Block a user