mirror of
https://github.com/darksaid98/new-phone-who-dis.git
synced 2026-12-04 22:16:40 +00:00
Tweak/use sound mount before its ready to play (#128)
* Make useSound use soundProvider and be sure its mounted! * Remove console.log
This commit is contained in:
1 parent
e9e22ce9bc
commit
a579d7bb8d
6 files changed
+207
-165
No files matched your search
@@ -16,7 +16,16 @@ export const ExampleApp = () => {
|
|||||||
const exampleString = useExample();
|
const exampleString = useExample();
|
||||||
const example = useApp('EXAMPLE');
|
const example = useApp('EXAMPLE');
|
||||||
const [soundName, setSound] = useState('pixel.ogg');
|
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 (
|
return (
|
||||||
<ExampleThemeProvider>
|
<ExampleThemeProvider>
|
||||||
<AppWrapper>
|
<AppWrapper>
|
||||||
@@ -29,9 +38,7 @@ export const ExampleApp = () => {
|
|||||||
<IconButton onClick={() => (playing ? stop() : play())}>
|
<IconButton onClick={() => (playing ? stop() : play())}>
|
||||||
{playing ? <StopIcon /> : <PlayIcon />}
|
{playing ? <StopIcon /> : <PlayIcon />}
|
||||||
</IconButton>
|
</IconButton>
|
||||||
<Button onClick={() => setSound('marimba.ogg')}>
|
<Button onClick={toggleSound}>Change sound on the fly</Button>
|
||||||
Change sound on the fly
|
|
||||||
</Button>
|
|
||||||
</AppContent>
|
</AppContent>
|
||||||
</AppWrapper>
|
</AppWrapper>
|
||||||
</ExampleThemeProvider>
|
</ExampleThemeProvider>
|
||||||
|
|||||||
@@ -5,12 +5,11 @@ import React, {
|
|||||||
useMemo,
|
useMemo,
|
||||||
useRef,
|
useRef,
|
||||||
useEffect,
|
useEffect,
|
||||||
useContext,
|
|
||||||
} from 'react';
|
} from 'react';
|
||||||
import { useRecoilValue } from 'recoil';
|
import { useRecoilValue } from 'recoil';
|
||||||
import { useSettings } from '../../../apps/settings/hooks/useSettings';
|
import { useSettings } from '../../../apps/settings/hooks/useSettings';
|
||||||
import { phoneState } from '../../phone/hooks/state';
|
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 { getSoundSettings } from '../../sound/utils/getSoundSettings';
|
||||||
import { DEFAULT_ALERT_HIDE_TIME } from '../notifications.constants';
|
import { DEFAULT_ALERT_HIDE_TIME } from '../notifications.constants';
|
||||||
|
|
||||||
@@ -70,7 +69,7 @@ export function NotificationsProvider({ children }) {
|
|||||||
|
|
||||||
const [notifications, setNotifications] = useState<INotification[]>([]);
|
const [notifications, setNotifications] = useState<INotification[]>([]);
|
||||||
|
|
||||||
const { mount, play } = useContext(soundContext);
|
const { mount, play } = useSoundProvider();
|
||||||
|
|
||||||
const alertTimeout = useRef<NodeJS.Timeout>();
|
const alertTimeout = useRef<NodeJS.Timeout>();
|
||||||
const [alerts, setAlerts] = useState<
|
const [alerts, setAlerts] = useState<
|
||||||
|
|||||||
@@ -1,14 +1,14 @@
|
|||||||
import { useContext, useEffect, useMemo } from 'react';
|
import { useEffect, useMemo } from 'react';
|
||||||
import { useSettings } from '../../../apps/settings/hooks/useSettings';
|
import { useSettings } from '../../../apps/settings/hooks/useSettings';
|
||||||
import { soundContext } from '../providers/SoundProvider';
|
|
||||||
import { getSoundSettings } from '../utils/getSoundSettings';
|
import { getSoundSettings } from '../utils/getSoundSettings';
|
||||||
|
import { useSoundProvider } from './useSoundProvider';
|
||||||
|
|
||||||
export const useRingtoneSound = () => {
|
export const useRingtoneSound = () => {
|
||||||
const [settings] = useSettings();
|
const [settings] = useSettings();
|
||||||
|
|
||||||
const context = useContext(soundContext);
|
const sound = useSoundProvider();
|
||||||
|
|
||||||
if (!context) {
|
if (!sound) {
|
||||||
throw new Error('useRingtoneSound must be wrapped in SoundProvider');
|
throw new Error('useRingtoneSound must be wrapped in SoundProvider');
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -17,16 +17,16 @@ export const useRingtoneSound = () => {
|
|||||||
]);
|
]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!context.isMounted(options.sound)) {
|
if (!sound.isMounted(options.sound)) {
|
||||||
context.mount(options.sound, options.volume, true);
|
sound.mount(options.sound, options.volume, true);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
context.volume(options.sound, options.volume);
|
sound.volume(options.sound, options.volume);
|
||||||
}, [context, options.sound, options.volume]);
|
}, [sound, options.sound, options.volume]);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
play: () => context.play(options.sound, options.volume, true),
|
play: () => sound.play(options.sound, options.volume, true),
|
||||||
stop: () => context.stop(options.sound),
|
stop: () => sound.stop(options.sound),
|
||||||
playing: () => context.playing(options.sound),
|
playing: () => sound.playing(options.sound),
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
import { useState, useEffect, useRef, useCallback } from 'react';
|
import { useEffect, useState } from 'react';
|
||||||
import { Howl } from 'howler';
|
import { usePreviousState } from '../../../common/utils/usePreviousState';
|
||||||
|
import { useSoundProvider } from './useSoundProvider';
|
||||||
|
|
||||||
interface ISoundOptions {
|
interface ISoundOptions {
|
||||||
volume?: number;
|
volume?: number;
|
||||||
@@ -7,83 +8,84 @@ interface ISoundOptions {
|
|||||||
loop?: boolean;
|
loop?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const DEFAULT_OPTIONS = { volume: 1, interrupt: false, loop: false };
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A hook allowing to play sound
|
* A hook allowing to play sound
|
||||||
* @param url The url of the sound you would like to play
|
* @param url The url of the sound you would like to play
|
||||||
*
|
*
|
||||||
* @param { volume, interrupt, loop} Additional options
|
* @param { volume, interrupt, loop} Additional options
|
||||||
**/
|
**/
|
||||||
const useSound = (
|
const useSound = (url: string, options: ISoundOptions = DEFAULT_OPTIONS) => {
|
||||||
url: string,
|
const { volume: vol, loop: isLoop, interrupt } = options;
|
||||||
{ volume = 1, interrupt = false, loop = false }: ISoundOptions = {}
|
|
||||||
) => {
|
|
||||||
const [isMounted, setMounted] = useState<boolean>(false);
|
|
||||||
const soundRef = useRef<Howl>(null);
|
|
||||||
|
|
||||||
const [playing, setPlaying] = useState<boolean>(false);
|
const [isPlaying, setPlaying] = useState<boolean>(false);
|
||||||
|
|
||||||
// Mount & Dismount handling
|
const {
|
||||||
useEffect(() => {
|
mount,
|
||||||
soundRef.current?.unload();
|
play,
|
||||||
setPlaying(false);
|
stop,
|
||||||
setMounted(false);
|
playing,
|
||||||
|
volume,
|
||||||
|
loop,
|
||||||
|
remove,
|
||||||
|
isMounted,
|
||||||
|
} = useSoundProvider();
|
||||||
|
|
||||||
soundRef.current = new Howl({
|
const previousInterrupt = usePreviousState(interrupt);
|
||||||
src: url,
|
const previousUrl = usePreviousState(url);
|
||||||
volume: 1,
|
|
||||||
loop: false,
|
|
||||||
});
|
|
||||||
|
|
||||||
setMounted(true);
|
|
||||||
|
|
||||||
return () => {
|
|
||||||
soundRef.current?.unload();
|
|
||||||
soundRef.current = undefined;
|
|
||||||
};
|
|
||||||
}, [url]);
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (isMounted) {
|
const changed = previousUrl !== url;
|
||||||
soundRef.current.volume(volume);
|
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(() => {
|
useEffect(() => {
|
||||||
if (isMounted) {
|
if (interrupt) {
|
||||||
soundRef.current.loop(loop);
|
stop(url);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
}, [loop, isMounted]);
|
if (!playing(url) && previousInterrupt) {
|
||||||
|
play();
|
||||||
|
}
|
||||||
|
}, [interrupt, play, playing, previousInterrupt, stop, url]);
|
||||||
|
|
||||||
const play = useCallback(() => {
|
return {
|
||||||
if (!soundRef.current) return;
|
play: () => {
|
||||||
|
play(url);
|
||||||
if (isMounted) {
|
|
||||||
soundRef.current.play();
|
|
||||||
soundRef.current.once('end', () => {
|
|
||||||
if (!soundRef.current.playing()) {
|
|
||||||
setPlaying(false);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
setPlaying(true);
|
setPlaying(true);
|
||||||
}
|
},
|
||||||
}, [isMounted]);
|
stop: () => {
|
||||||
|
stop(url);
|
||||||
const stop = useCallback(() => {
|
|
||||||
if (!soundRef.current) return;
|
|
||||||
|
|
||||||
if (isMounted) {
|
|
||||||
soundRef.current.stop();
|
|
||||||
setPlaying(false);
|
setPlaying(false);
|
||||||
}
|
},
|
||||||
}, [isMounted]);
|
playing: isPlaying,
|
||||||
|
};
|
||||||
useEffect(() => {
|
|
||||||
if (interrupt && isMounted) {
|
|
||||||
stop();
|
|
||||||
}
|
|
||||||
}, [interrupt, isMounted, stop]);
|
|
||||||
|
|
||||||
return { play, playing, stop };
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export default useSound;
|
export default useSound;
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
import { useContext } from 'react';
|
||||||
|
import { soundContext } from '../providers/SoundProvider';
|
||||||
|
|
||||||
|
export const useSoundProvider = () => {
|
||||||
|
return useContext(soundContext);
|
||||||
|
};
|
||||||
@@ -17,6 +17,7 @@ interface ISoundContext {
|
|||||||
stop(url: string): void;
|
stop(url: string): void;
|
||||||
remove(url: string): boolean;
|
remove(url: string): boolean;
|
||||||
volume(url: string, volume?: number): number;
|
volume(url: string, volume?: number): number;
|
||||||
|
loop(url: string, loop?: boolean): boolean;
|
||||||
playing(url: string): boolean;
|
playing(url: string): boolean;
|
||||||
isMounted(url: string): boolean;
|
isMounted(url: string): boolean;
|
||||||
}
|
}
|
||||||
@@ -30,98 +31,122 @@ export const SoundProvider = ({ children }: { children: React.ReactNode }) => {
|
|||||||
soundRefs.current = new Map();
|
soundRefs.current = new Map();
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
const mount = (
|
const mount = useCallback(
|
||||||
url: string,
|
(
|
||||||
volume: number = 1,
|
url: string,
|
||||||
loop: boolean = false,
|
volume: number = 1,
|
||||||
autoplay: boolean = false
|
loop: boolean = false,
|
||||||
) => {
|
autoplay: boolean = false
|
||||||
return new Promise<IMountResponse>((res) => {
|
) => {
|
||||||
if (!soundRefs.current) {
|
return new Promise<IMountResponse>((res) => {
|
||||||
soundRefs.current = new Map();
|
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();
|
|
||||||
}
|
}
|
||||||
return res({ howl: find.howl, url });
|
|
||||||
}
|
|
||||||
|
|
||||||
const instance = new Howl({
|
const find = soundRefs.current.get(url);
|
||||||
src: url,
|
|
||||||
loop,
|
|
||||||
volume,
|
|
||||||
autoplay,
|
|
||||||
});
|
|
||||||
|
|
||||||
soundRefs.current.set(url, { howl: instance, volume });
|
if (find) {
|
||||||
|
find.howl.volume(volume);
|
||||||
instance.once('load', () => {
|
find.howl.loop(loop);
|
||||||
console.log('ready', instance);
|
if (autoplay) {
|
||||||
res({ howl: instance, url });
|
find.howl.play();
|
||||||
});
|
}
|
||||||
|
return res({ howl: find.howl, 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', () => {
|
const instance = new Howl({
|
||||||
fadedSounds.forEach((s) => {
|
src: url,
|
||||||
s.howl.fade(s.howl.volume(), s.volume, 50);
|
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;
|
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);
|
const sound = soundRefs.current.get(url);
|
||||||
sound?.howl.stop();
|
sound?.howl.stop();
|
||||||
};
|
}, []);
|
||||||
|
|
||||||
const remove = (url: string): boolean => {
|
const remove = useCallback((url: string): boolean => {
|
||||||
|
if (!soundRefs.current) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
const sound = soundRefs.current.get(url);
|
const sound = soundRefs.current.get(url);
|
||||||
if (sound) {
|
if (sound) {
|
||||||
sound.howl.stop();
|
sound.howl.stop();
|
||||||
@@ -130,23 +155,26 @@ export const SoundProvider = ({ children }: { children: React.ReactNode }) => {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
};
|
}, []);
|
||||||
|
|
||||||
const isMounted = (url: string): boolean => {
|
const isMounted = useCallback((url: string): boolean => {
|
||||||
if (!soundRefs.current) {
|
if (!soundRefs.current) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return !!soundRefs.current.get(url);
|
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);
|
const sound = soundRefs.current.get(url);
|
||||||
return sound?.howl.playing() || false;
|
return sound?.howl.playing() || false;
|
||||||
};
|
}, []);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<soundContext.Provider
|
<soundContext.Provider
|
||||||
value={{ mount, play, stop, playing, remove, volume, isMounted }}
|
value={{ mount, play, stop, playing, remove, volume, loop, isMounted }}
|
||||||
>
|
>
|
||||||
{children}
|
{children}
|
||||||
</soundContext.Provider>
|
</soundContext.Provider>
|
||||||
|
|||||||
Reference in new issue
Block a user