mirror of
https://github.com/darksaid98/new-phone-who-dis.git
synced 2026-12-03 14:38:45 +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 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 (
|
||||
<ExampleThemeProvider>
|
||||
<AppWrapper>
|
||||
@@ -29,9 +38,7 @@ export const ExampleApp = () => {
|
||||
<IconButton onClick={() => (playing ? stop() : play())}>
|
||||
{playing ? <StopIcon /> : <PlayIcon />}
|
||||
</IconButton>
|
||||
<Button onClick={() => setSound('marimba.ogg')}>
|
||||
Change sound on the fly
|
||||
</Button>
|
||||
<Button onClick={toggleSound}>Change sound on the fly</Button>
|
||||
</AppContent>
|
||||
</AppWrapper>
|
||||
</ExampleThemeProvider>
|
||||
|
||||
@@ -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<INotification[]>([]);
|
||||
|
||||
const { mount, play } = useContext(soundContext);
|
||||
const { mount, play } = useSoundProvider();
|
||||
|
||||
const alertTimeout = useRef<NodeJS.Timeout>();
|
||||
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 { 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),
|
||||
};
|
||||
};
|
||||
@@ -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<boolean>(false);
|
||||
const soundRef = useRef<Howl>(null);
|
||||
const useSound = (url: string, options: ISoundOptions = DEFAULT_OPTIONS) => {
|
||||
const { volume: vol, loop: isLoop, interrupt } = options;
|
||||
|
||||
const [playing, setPlaying] = useState<boolean>(false);
|
||||
const [isPlaying, setPlaying] = useState<boolean>(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;
|
||||
@@ -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;
|
||||
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<IMountResponse>((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<IMountResponse>((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 (
|
||||
<soundContext.Provider
|
||||
value={{ mount, play, stop, playing, remove, volume, isMounted }}
|
||||
value={{ mount, play, stop, playing, remove, volume, loop, isMounted }}
|
||||
>
|
||||
{children}
|
||||
</soundContext.Provider>
|
||||
|
||||
Reference in new issue
Block a user