diff --git a/phone/src/apps/example/components/ExampleApp.tsx b/phone/src/apps/example/components/ExampleApp.tsx index ec27821..2f5fc69 100644 --- a/phone/src/apps/example/components/ExampleApp.tsx +++ b/phone/src/apps/example/components/ExampleApp.tsx @@ -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 ( @@ -21,6 +26,12 @@ export const ExampleApp = () => { {/* Here we are using the value in a h3 tag */}

{exampleString}

+ (playing ? stop() : play())}> + {playing ? : } + +
diff --git a/phone/src/os/phone/hooks/useSound.ts b/phone/src/os/phone/hooks/useSound.ts index 27efa1c..e1cf491 100644 --- a/phone/src/os/phone/hooks/useSound.ts +++ b/phone/src/os/phone/hooks/useSound.ts @@ -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(false); + const [isMounted, setMounted] = useState(false); + const soundRef = useRef(null); const [playing, setPlaying] = useState(false); - const [sound, setSound] = useState(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 }; };