mirror of
https://github.com/darksaid98/new-phone-who-dis.git
synced 2026-12-03 06:28:51 +00:00
Merge pull request #94 from project-error/feat/useSound
feat(phone/os): useSound hook
This commit is contained in:
4 files changed
+99
-6
No files matched your search
+2
-5
@@ -15,9 +15,6 @@ ui_page 'resources/html/index.html'
|
||||
|
||||
files {
|
||||
'resources/html/index.html',
|
||||
'resources/html/*.js',
|
||||
'resources/html/media/frames/*.png',
|
||||
'resources/html/media/backgrounds/*.png',
|
||||
'resources/html/media/backgrounds/*.jpg',
|
||||
'resources/html/media/twitter/*'
|
||||
'resources/html/main.js',
|
||||
'resources/html/media/**/*'
|
||||
}
|
||||
@@ -25,6 +25,7 @@
|
||||
"emoji-mart": "^3.0.0",
|
||||
"enzyme": "^3.11.0",
|
||||
"enzyme-adapter-react-16": "^1.15.5",
|
||||
"howler": "^2.2.1",
|
||||
"i18next": "^19.7.0",
|
||||
"parse-url": "^5.0.2",
|
||||
"prettier": "^2.0.5",
|
||||
@@ -47,6 +48,7 @@
|
||||
"@babel/preset-env": "^7.4.5",
|
||||
"@babel/preset-react": "^7.0.0",
|
||||
"@types/enzyme": "^3.10.7",
|
||||
"@types/howler": "^2.2.1",
|
||||
"@types/node": "^14.11.5",
|
||||
"@types/qs": "^6.9.5",
|
||||
"@types/react": "^16.9.51",
|
||||
|
||||
@@ -0,0 +1,84 @@
|
||||
import { useState, useEffect, useRef, useCallback } from 'react';
|
||||
import { Howl } from 'howler';
|
||||
|
||||
interface ISoundOptions {
|
||||
volume?: number;
|
||||
interrupt?: boolean;
|
||||
loop?: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 = useRef<boolean>(false);
|
||||
|
||||
const [playing, setPlaying] = useState<boolean>(false);
|
||||
|
||||
const [sound, setSound] = useState<Howl | null>(null);
|
||||
|
||||
// Mount & Dismount handling
|
||||
useEffect(() => {
|
||||
isMounted.current = true;
|
||||
const sound = new Howl({
|
||||
src: url,
|
||||
volume,
|
||||
loop,
|
||||
});
|
||||
setSound(sound);
|
||||
|
||||
return () => {
|
||||
isMounted.current = false;
|
||||
};
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
if (sound) {
|
||||
sound.volume(volume);
|
||||
}
|
||||
// Shouldnt need to rerun if sound changes
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [volume]);
|
||||
|
||||
const play = useCallback(() => {
|
||||
if (!sound) return;
|
||||
|
||||
if (interrupt) return sound.stop();
|
||||
|
||||
sound.play();
|
||||
|
||||
if (isMounted.current) {
|
||||
sound.once('end', () => {
|
||||
if (!sound.playing()) {
|
||||
setPlaying(false);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
if (isMounted.current) {
|
||||
setPlaying(true);
|
||||
}
|
||||
}, [interrupt, sound]);
|
||||
|
||||
const stop = useCallback(() => {
|
||||
if (!sound) return;
|
||||
|
||||
sound.stop();
|
||||
|
||||
if (isMounted.current) {
|
||||
setPlaying(false);
|
||||
}
|
||||
}, [sound]);
|
||||
|
||||
return { play, playing, stop };
|
||||
};
|
||||
|
||||
export default useSound;
|
||||
+11
-1
@@ -1926,6 +1926,11 @@
|
||||
resolved "https://registry.yarnpkg.com/@types/history/-/history-4.7.8.tgz#49348387983075705fe8f4e02fb67f7daaec4934"
|
||||
integrity sha512-S78QIYirQcUoo6UJZx9CSP0O2ix9IaeAXwQi26Rhr/+mg7qqPy8TzaxHSUut7eGjL8WmLccT7/MXf304WjqHcA==
|
||||
|
||||
"@types/howler@^2.2.1":
|
||||
version "2.2.1"
|
||||
resolved "https://registry.yarnpkg.com/@types/howler/-/howler-2.2.1.tgz#c95314724a8e970b1b29a731861975f39987b9ca"
|
||||
integrity sha512-1MiSldngr+eAO4lDPtjzl4Nf2GmRh8VDHIpNBIkyd25L22JExVlI6w3fjSM7+FNc1e1WZAPNq7/flkw685byfg==
|
||||
|
||||
"@types/html-minifier-terser@^5.0.0":
|
||||
version "5.1.1"
|
||||
resolved "https://registry.yarnpkg.com/@types/html-minifier-terser/-/html-minifier-terser-5.1.1.tgz#3c9ee980f1a10d6021ae6632ca3e79ca2ec4fb50"
|
||||
@@ -6473,6 +6478,11 @@ hosted-git-info@^2.1.4:
|
||||
resolved "https://registry.yarnpkg.com/hosted-git-info/-/hosted-git-info-2.8.8.tgz#7539bd4bc1e0e0a895815a2e0262420b12858488"
|
||||
integrity sha512-f/wzC2QaWBs7t9IYqB4T3sR1xviIViXJRJTWBlx2Gf3g0Xi5vI7Yy4koXQ1c9OYDGHN9sBy1DQ2AB8fqZBWhUg==
|
||||
|
||||
howler@^2.2.1:
|
||||
version "2.2.1"
|
||||
resolved "https://registry.yarnpkg.com/howler/-/howler-2.2.1.tgz#a521a9b495841e8bb9aa12e651bebba0affc179e"
|
||||
integrity sha512-0iIXvuBO/81CcrQ/HSSweYmbT50fT2mIc9XMFb+kxIfk2pW/iKzDbX1n3fZmDXMEIpYvyyfrB+gXwPYSDqUxIQ==
|
||||
|
||||
hpack.js@^2.1.6:
|
||||
version "2.1.6"
|
||||
resolved "https://registry.yarnpkg.com/hpack.js/-/hpack.js-2.1.6.tgz#87774c0949e513f42e84575b3c45681fade2a0b2"
|
||||
@@ -9348,7 +9358,7 @@ p-cancelable@^1.0.0:
|
||||
version "1.1.0"
|
||||
resolved "https://registry.yarnpkg.com/p-cancelable/-/p-cancelable-1.1.0.tgz#d078d15a3af409220c886f1d9a0ca2e441ab26cc"
|
||||
integrity sha512-s73XxOZ4zpt1edZYZzvhqFa6uvQc1vwUa0K0BdtIZgQMAJj9IbebH+JkgKZc9h+B05PKHLOTl4ajG1BmNrVZlw==
|
||||
|
||||
|
||||
p-defer@^1.0.0:
|
||||
version "1.0.0"
|
||||
resolved "https://registry.yarnpkg.com/p-defer/-/p-defer-1.0.0.tgz#9f6eb182f6c9aa8cd743004a7d4f96b196b0fb0c"
|
||||
|
||||
Reference in new issue
Block a user