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:
J Francisco Rader authored and GitHub committed 2021-03-01 22:13:44 -05:00
1 parent e9e22ce9bc
commit a579d7bb8d
6 files changed
+207 -165

No files matched your search

+114 -86
View File
@@ -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>