From a579d7bb8d5f082fb5cea378533f379341b90570 Mon Sep 17 00:00:00 2001 From: J Francisco Rader Date: Mon, 1 Mar 2021 22:13:44 -0500 Subject: [PATCH] Tweak/use sound mount before its ready to play (#128) * Make useSound use soundProvider and be sure its mounted! * Remove console.log --- .../apps/example/components/ExampleApp.tsx | 15 +- .../providers/NotificationsProvider.tsx | 5 +- phone/src/os/sound/hooks/useRingtoneSound.ts | 22 +- phone/src/os/sound/hooks/useSound.ts | 124 +++++------ phone/src/os/sound/hooks/useSoundProvider.tsx | 6 + .../src/os/sound/providers/SoundProvider.tsx | 200 ++++++++++-------- 6 files changed, 207 insertions(+), 165 deletions(-) create mode 100644 phone/src/os/sound/hooks/useSoundProvider.tsx diff --git a/phone/src/apps/example/components/ExampleApp.tsx b/phone/src/apps/example/components/ExampleApp.tsx index 07d96e0..7d42378 100644 --- a/phone/src/apps/example/components/ExampleApp.tsx +++ b/phone/src/apps/example/components/ExampleApp.tsx @@ -16,7 +16,16 @@ export const ExampleApp = () => { const exampleString = useExample(); const example = useApp('EXAMPLE'); const [soundName, setSound] = useState('pixel.ogg'); - const { play, playing, stop } = useSound('media/ringtones/' + soundName); + const { play, playing, stop } = useSound('./media/ringtones/' + soundName); + + const toggleSound = () => { + if (soundName === 'pixel.ogg') { + setSound('marimba.ogg'); + return; + } + setSound('pixel.ogg'); + }; + return ( @@ -29,9 +38,7 @@ export const ExampleApp = () => { (playing ? stop() : play())}> {playing ? : } - + diff --git a/phone/src/os/notifications/providers/NotificationsProvider.tsx b/phone/src/os/notifications/providers/NotificationsProvider.tsx index 43d69d7..90f6158 100644 --- a/phone/src/os/notifications/providers/NotificationsProvider.tsx +++ b/phone/src/os/notifications/providers/NotificationsProvider.tsx @@ -5,12 +5,11 @@ import React, { useMemo, useRef, useEffect, - useContext, } from 'react'; import { useRecoilValue } from 'recoil'; import { useSettings } from '../../../apps/settings/hooks/useSettings'; import { phoneState } from '../../phone/hooks/state'; -import { soundContext } from '../../sound/providers/SoundProvider'; +import { useSoundProvider } from '../../sound/hooks/useSoundProvider'; import { getSoundSettings } from '../../sound/utils/getSoundSettings'; import { DEFAULT_ALERT_HIDE_TIME } from '../notifications.constants'; @@ -70,7 +69,7 @@ export function NotificationsProvider({ children }) { const [notifications, setNotifications] = useState([]); - const { mount, play } = useContext(soundContext); + const { mount, play } = useSoundProvider(); const alertTimeout = useRef(); const [alerts, setAlerts] = useState< diff --git a/phone/src/os/sound/hooks/useRingtoneSound.ts b/phone/src/os/sound/hooks/useRingtoneSound.ts index a016d97..396cfe2 100644 --- a/phone/src/os/sound/hooks/useRingtoneSound.ts +++ b/phone/src/os/sound/hooks/useRingtoneSound.ts @@ -1,14 +1,14 @@ -import { useContext, useEffect, useMemo } from 'react'; +import { useEffect, useMemo } from 'react'; import { useSettings } from '../../../apps/settings/hooks/useSettings'; -import { soundContext } from '../providers/SoundProvider'; import { getSoundSettings } from '../utils/getSoundSettings'; +import { useSoundProvider } from './useSoundProvider'; export const useRingtoneSound = () => { const [settings] = useSettings(); - const context = useContext(soundContext); + const sound = useSoundProvider(); - if (!context) { + if (!sound) { throw new Error('useRingtoneSound must be wrapped in SoundProvider'); } @@ -17,16 +17,16 @@ export const useRingtoneSound = () => { ]); useEffect(() => { - if (!context.isMounted(options.sound)) { - context.mount(options.sound, options.volume, true); + if (!sound.isMounted(options.sound)) { + sound.mount(options.sound, options.volume, true); return; } - context.volume(options.sound, options.volume); - }, [context, options.sound, options.volume]); + sound.volume(options.sound, options.volume); + }, [sound, options.sound, options.volume]); return { - play: () => context.play(options.sound, options.volume, true), - stop: () => context.stop(options.sound), - playing: () => context.playing(options.sound), + play: () => sound.play(options.sound, options.volume, true), + stop: () => sound.stop(options.sound), + playing: () => sound.playing(options.sound), }; }; diff --git a/phone/src/os/sound/hooks/useSound.ts b/phone/src/os/sound/hooks/useSound.ts index e1cf491..fc55698 100644 --- a/phone/src/os/sound/hooks/useSound.ts +++ b/phone/src/os/sound/hooks/useSound.ts @@ -1,5 +1,6 @@ -import { useState, useEffect, useRef, useCallback } from 'react'; -import { Howl } from 'howler'; +import { useEffect, useState } from 'react'; +import { usePreviousState } from '../../../common/utils/usePreviousState'; +import { useSoundProvider } from './useSoundProvider'; interface ISoundOptions { volume?: number; @@ -7,83 +8,84 @@ interface ISoundOptions { loop?: boolean; } +const DEFAULT_OPTIONS = { volume: 1, interrupt: false, loop: false }; + /** * A hook allowing to play sound * @param url The url of the sound you would like to play * * @param { volume, interrupt, loop} Additional options **/ -const useSound = ( - url: string, - { volume = 1, interrupt = false, loop = false }: ISoundOptions = {} -) => { - const [isMounted, setMounted] = useState(false); - const soundRef = useRef(null); +const useSound = (url: string, options: ISoundOptions = DEFAULT_OPTIONS) => { + const { volume: vol, loop: isLoop, interrupt } = options; - const [playing, setPlaying] = useState(false); + const [isPlaying, setPlaying] = useState(false); - // Mount & Dismount handling - useEffect(() => { - soundRef.current?.unload(); - setPlaying(false); - setMounted(false); + const { + mount, + play, + stop, + playing, + volume, + loop, + remove, + isMounted, + } = useSoundProvider(); - soundRef.current = new Howl({ - src: url, - volume: 1, - loop: false, - }); - - setMounted(true); - - return () => { - soundRef.current?.unload(); - soundRef.current = undefined; - }; - }, [url]); + const previousInterrupt = usePreviousState(interrupt); + const previousUrl = usePreviousState(url); useEffect(() => { - if (isMounted) { - soundRef.current.volume(volume); + const changed = previousUrl !== url; + if (changed) { + stop(previousUrl); + remove(previousUrl); } - }, [volume, isMounted]); + if (!isMounted(url)) { + mount(url, vol, isLoop, changed && isPlaying); + return; + } + if (changed && isPlaying) { + play(url); + } + volume(url, vol); + loop(url, isLoop); + }, [ + url, + vol, + isLoop, + isMounted, + volume, + loop, + mount, + previousUrl, + stop, + remove, + isPlaying, + play, + ]); useEffect(() => { - if (isMounted) { - soundRef.current.loop(loop); + if (interrupt) { + stop(url); + return; } - }, [loop, isMounted]); + if (!playing(url) && previousInterrupt) { + play(); + } + }, [interrupt, play, playing, previousInterrupt, stop, url]); - const play = useCallback(() => { - if (!soundRef.current) return; - - if (isMounted) { - soundRef.current.play(); - soundRef.current.once('end', () => { - if (!soundRef.current.playing()) { - setPlaying(false); - } - }); + return { + play: () => { + play(url); setPlaying(true); - } - }, [isMounted]); - - const stop = useCallback(() => { - if (!soundRef.current) return; - - if (isMounted) { - soundRef.current.stop(); + }, + stop: () => { + stop(url); setPlaying(false); - } - }, [isMounted]); - - useEffect(() => { - if (interrupt && isMounted) { - stop(); - } - }, [interrupt, isMounted, stop]); - - return { play, playing, stop }; + }, + playing: isPlaying, + }; }; export default useSound; diff --git a/phone/src/os/sound/hooks/useSoundProvider.tsx b/phone/src/os/sound/hooks/useSoundProvider.tsx new file mode 100644 index 0000000..537d4f1 --- /dev/null +++ b/phone/src/os/sound/hooks/useSoundProvider.tsx @@ -0,0 +1,6 @@ +import { useContext } from 'react'; +import { soundContext } from '../providers/SoundProvider'; + +export const useSoundProvider = () => { + return useContext(soundContext); +}; diff --git a/phone/src/os/sound/providers/SoundProvider.tsx b/phone/src/os/sound/providers/SoundProvider.tsx index f620851..60fb1c4 100644 --- a/phone/src/os/sound/providers/SoundProvider.tsx +++ b/phone/src/os/sound/providers/SoundProvider.tsx @@ -17,6 +17,7 @@ interface ISoundContext { stop(url: string): void; remove(url: string): boolean; volume(url: string, volume?: number): number; + loop(url: string, loop?: boolean): boolean; playing(url: string): boolean; isMounted(url: string): boolean; } @@ -30,98 +31,122 @@ export const SoundProvider = ({ children }: { children: React.ReactNode }) => { soundRefs.current = new Map(); }, []); - const mount = ( - url: string, - volume: number = 1, - loop: boolean = false, - autoplay: boolean = false - ) => { - return new Promise((res) => { - if (!soundRefs.current) { - soundRefs.current = new Map(); - } - - const find = soundRefs.current.get(url); - - if (find) { - find.howl.volume(volume); - find.howl.loop(loop); - if (autoplay) { - find.howl.play(); + const mount = useCallback( + ( + url: string, + volume: number = 1, + loop: boolean = false, + autoplay: boolean = false + ) => { + return new Promise((res) => { + if (!soundRefs.current) { + soundRefs.current = new Map(); } - return res({ howl: find.howl, url }); - } - const instance = new Howl({ - src: url, - loop, - volume, - autoplay, - }); + const find = soundRefs.current.get(url); - soundRefs.current.set(url, { howl: instance, volume }); - - instance.once('load', () => { - console.log('ready', instance); - res({ howl: instance, url }); - }); - - const onError = (_id, e) => { - res(null); - console.error('Howler Error \n', e, '\nid: ', _id, '\nsrc: ', url); - }; - - instance.once('loaderror', onError); - - instance.once('playerror', onError); - }); - }; - - const play = useCallback((url: string, volume: number, loop: boolean) => { - const sound = soundRefs.current.get(url); - let fadedSounds = []; - if (sound) { - soundRefs.current.forEach((ref, key) => { - const isCurrent = key === url; - const isPlaying = ref.howl.playing(); - const isLoop = ref.howl.loop(); - if (isCurrent || !isLoop || !isPlaying) { - return; + if (find) { + find.howl.volume(volume); + find.howl.loop(loop); + if (autoplay) { + find.howl.play(); + } + return res({ howl: find.howl, url }); } - fadedSounds.push(ref); - const currVol = ref.howl.volume(); - ref.howl.fade(currVol, currVol * 0.3, 50); - }); - if (volume !== undefined) { - sound.howl.volume(volume); - } - if (loop !== undefined) { - sound.howl.loop(loop); - } - sound.howl.play(); - sound.howl.once('end', () => { - fadedSounds.forEach((s) => { - s.howl.fade(s.howl.volume(), s.volume, 50); + const instance = new Howl({ + src: url, + loop, + volume, + autoplay, }); - fadedSounds = undefined; + + soundRefs.current.set(url, { howl: instance, volume }); + + instance.once('load', () => { + console.log('ready', instance); + res({ howl: instance, url }); + }); + + const onError = (_id, e) => { + res(null); + console.error('Howler Error \n', e, '\nid: ', _id, '\nsrc: ', url); + }; + + instance.once('loaderror', onError); + + instance.once('playerror', onError); }); + }, + [] + ); + + const play = useCallback( + (url: string, volume: number, loop: boolean) => { + const sound = soundRefs.current.get(url); + let fadedSounds = []; + if (sound) { + soundRefs.current.forEach((ref, key) => { + const isCurrent = key === url; + const isPlaying = ref.howl.playing(); + const isLoop = ref.howl.loop(); + if (isCurrent || !isLoop || !isPlaying) { + return; + } + fadedSounds.push(ref); + const currVol = ref.howl.volume(); + ref.howl.fade(currVol, currVol * 0.3, 50); + }); + if (volume !== undefined) { + sound.howl.volume(volume); + } + if (loop !== undefined) { + sound.howl.loop(loop); + } + sound.howl.play(); + + sound.howl.once('end', () => { + fadedSounds.forEach((s) => { + s.howl.fade(s.howl.volume(), s.volume, 50); + }); + fadedSounds = undefined; + }); + return; + } + mount(url, volume, loop, true); + }, + [mount] + ); + + const volume = useCallback( + (url: string, volume: number = undefined): number => { + const sound = soundRefs.current.get(url); + return sound?.howl.volume(volume) as number; + }, + [] + ); + + const loop = useCallback( + (url: string, loop: boolean = undefined): boolean => { + const sound = soundRefs.current.get(url); + sound.howl.loop(loop); + return sound.howl.loop(); + }, + [] + ); + + const stop = useCallback((url: string) => { + if (!soundRefs.current) { return; } - mount(url, volume, loop, true); - }, []); - - const volume = (url: string, volume: number = undefined): number => { - const sound = soundRefs.current.get(url); - return sound?.howl.volume(volume) as number; - }; - - const stop = (url: string) => { const sound = soundRefs.current.get(url); sound?.howl.stop(); - }; + }, []); - const remove = (url: string): boolean => { + const remove = useCallback((url: string): boolean => { + if (!soundRefs.current) { + return; + } const sound = soundRefs.current.get(url); if (sound) { sound.howl.stop(); @@ -130,23 +155,26 @@ export const SoundProvider = ({ children }: { children: React.ReactNode }) => { return true; } return false; - }; + }, []); - const isMounted = (url: string): boolean => { + const isMounted = useCallback((url: string): boolean => { if (!soundRefs.current) { return false; } return !!soundRefs.current.get(url); - }; + }, []); - const playing = (url: string) => { + const playing = useCallback((url: string) => { + if (!soundRefs.current) { + return false; + } const sound = soundRefs.current.get(url); return sound?.howl.playing() || false; - }; + }, []); return ( {children}