Programming/React

[ React ] ์•„์ด์ฝ˜ ๋ˆ„๋ฅด๋ฉด ์Œ์•… ์žฌ์ƒ๋˜๋Š” ๊ธฐ๋Šฅ

seulye 2024. 1. 9. 08:55

 

์‚ฌ์ด๋“œ ํ”„๋กœ์ ํŠธ ์ค‘, ์•„์ด์ฝ˜์„ ๋ˆ„๋ฅด๋ฉด ์Œ์•…์ด ์žฌ์ƒ๋˜๋Š” ๊ธฐ๋Šฅ์ด ํ•„์š”ํ–ˆ๋‹ค. 

 

Off ์•„์ด์ฝ˜์œผ๋กœ ๋ณด์ด๋ฉด ์Œ์•…์ด ๊บผ์ ธ์žˆ๋‹ค๋Š” ๊ฒƒ์ด๊ณ ,

Up ์•„์ด์ฝ˜์œผ๋กœ ๋ณด์ด๋ฉด ์Œ์•…์ด ์‹คํ–‰์ค‘์ด๋ผ๋Š” ๋œป์ด๋‹ค. 

์ด ๋‘˜ ์•„์ด์ฝ˜์€ mui์—์„œ ๊ฐ€์ ธ์™”๋‹ค. 

const [isPlay, setIsPlay] = useState<boolean>(false);
const audio = new Audio(music);

  const onClickVolume = () => {
    // ์žฌ์ƒ ์ค‘
    if (isPlay) {
      audio.pause();
      setIsPlay(false);
    } else {
      audio.play();
      setIsPlay(true);
    }
  };

 

 

 

1. ์ฒซ ๋ฒˆ์งธ ์˜ค๋ฅ˜

Cannot find module 'mp3 ๊ฒฝ๋กœ' or its corresponding type declarations. 

 

https://stackoverflow.com/questions/71205222/how-to-fix-the-cannot-find-module-mp3-file-or-its-corresponding-type-declaration

 

How to fix the Cannot find module mp3 file or its corresponding type declarations

How to fix the error Cannot find module './audio/audio1.mp3' or its corresponding type declarations. also already use the require('./audio/audio1.mp3') and also getting an error.

stackoverflow.com

 

src/react-app-env.d.ts

์ด ํŒŒ์ผ์— ์ถ”๊ฐ€ํ•ด์ฃผ๋‹ˆ ์ •์ƒ์ ์œผ๋กœ ์žฌ์ƒ์ด ๋˜์—ˆ๋‹ค. 

declare module '*.mp3'

 

 

2. ์ค‘์ง€๊ฐ€ ์•ˆ ๋จ

pause๊ฐ€ ์ œ๋Œ€๋กœ ๋จน์ง€ ์•Š๋Š” ์˜ค๋ฅ˜๊ฐ€ ์žˆ์—ˆ๋‹ค. ์ค‘์ง€ํ•˜๋Š” ์˜๋ฏธ์˜ ์•„์ด์ฝ˜์„ ๋ˆŒ๋ €๋‹ค๊ฐ€ ๋‹ค์‹œ ์žฌ์ƒ์•„์ด์ฝ˜์„ ๋ˆ„๋ฅด์ž ์Œ์•…์ด ๊ฒน์žฌ์ƒ๋˜๋Š” ์ด์Šˆ๋„ ์žˆ์—ˆ๋‹ค. ์˜ˆ์ „์— ์นด์นด์˜ค๋งต API ์—ฐ๊ฒฐํ•  ๋•Œ๋„ ๋น„์Šทํ•œ ์ด์Šˆ๊ฐ€ ์žˆ์—ˆ๋‹ค. ์ดˆ๊ธฐ ๋ Œ๋”๋ง๊ณผ ์–ธ๋งˆ์šดํŠธ์‹œ ๋ฆฌ์Šค๋„ˆ ์ •๋ฆฌ ๋“ฑ์„ ํ•ด์ฃผ์–ด์•ผ ํ–ˆ๋‹ค. 

 

const [audio] = useState(new Audio(music)); // audio ๊ฐ์ฒด ์ƒ์„ฑ

  useEffect(() => {
    const handleEnded = () => {
      // ์Œ์•…์ด ๋๋‚ฌ์„ ๋•Œ ํ•  ์ž‘์—…
      setIsPlay(false);
    };

    audio.addEventListener("ended", handleEnded);

    // ์–ธ๋งˆ์šดํŠธ ์‹œ ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ ์ •๋ฆฌ
    return () => {
      audio.removeEventListener("ended", handleEnded);
    };
  }, [audio]);

 

์ด๋Ÿฐ์‹์œผ๋กœ ์ž‘์„ฑํ•ด์ฃผ๋ฉด ํ•ด๊ฒฐ์ด ๋œ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ.. React ์Šค๋Ÿฌ์šด ์ฝ”๋“œ๋Š” ์•„๋‹ˆ๋ผ๋Š” ์ƒ๊ฐ์ด ๋“ค์—ˆ๋‹ค. 

 

https://velog.io/@chocolajin/%EB%A6%AC%EC%95%A1%ED%8A%B8-%EC%98%A4%EB%94%94%EC%98%A4-%EC%BB%B4%ED%8F%AC%EB%84%8C%ED%8A%B8-%EB%A7%8C%EB%93%A4%EA%B8%B0-with-%ED%83%80%EC%9E%85%EC%8A%A4%ED%81%AC%EB%A6%BD%ED%8A%B8-%EB%A6%AC%EC%BD%94%EC%9D%BC

 

๋ฆฌ์•กํŠธ ์˜ค๋””์˜ค ์ปดํฌ๋„ŒํŠธ ๋งŒ๋“ค๊ธฐ with ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ & ๋ฆฌ์ฝ”์ผ

my-app ์ด๋ž€ ์ด๋ฆ„์˜ ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ๊ธฐ๋ฐ˜ ํ”„๋กœ์ ํŠธ ์ƒ์„ฑnpx create-react-app my-app --template typescript๋ฆฌ์ฝ”์ผ ์„ค์น˜npm install recoil์˜ค๋””์˜ค ํŒŒ์ผ์„ ๋“ค๊ณ ์˜ค๋ ค๋ฉด ์„ธ ๋‹จ๊ณ„๊ฐ€ ํ•„์š”ํ•˜๋‹ค.ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ ์„ ์–ธ ํŒŒ์ผ d.ts์„

velog.io

 

์ด ๊ฒŒ์‹œ๋ฌผ์„ ๋ณด๊ณ , ref์„ ์‚ฌ์šฉํ•˜์—ฌ audio ๊ฐ์ฒด๋ฅผ ๊ด€๋ฆฌ๋ฅผ ํ•ด์ฃผ์—ˆ๋‹ค. 

 

const myRef = useRef<HTMLAudioElement>(null);

  const onClickVolume = () => {
    // ์žฌ์ƒ ์ค‘
    if (isPlay) {
      myRef.current?.pause();
      setIsPlay(false);
    } else {
      // ์Œ์•… ์žฌ์ƒ
      myRef.current?.play();
      setIsPlay(true);
    }
  };
  
  return (
    <>
      <InvBGMBlock>
        <div className="volume__box" onClick={onClickVolume}>
          {isPlay && <VolumeUpIcon />}
          {!isPlay && <VolumeOffIcon />}
          <audio ref={myRef} src={music}></audio>
        </div>
      </InvBGMBlock>
   ...์ƒ๋žต

 

์ž˜ ๋œ๋‹ค! ๐Ÿ‘๐Ÿ‘