Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

detail 페이지에서 벗어나도 음악이 계속 재생되는 이슈 #166

Open
new-crystal opened this issue Aug 8, 2023 · 0 comments

Comments

@new-crystal
Copy link
Contributor

new-crystal commented Aug 8, 2023

2023-08-08.8.11.37.mov
  1. useEffect의 return 함수 사용하기
  2. removeEventListener 활용하기
  3. 코드
useEffect(() => {
    const audio = new Audio(playList);

    const timeUpdateHandler = () => {
      setCurrentTime(audio.currentTime);
      setRatio((audio.currentTime / audio.duration) * 100);
      if (audio.duration === audio.currentTime) {
        setPlaying(false);
      }
    };

    if (playing) {
      audio.play().then(() => {
        audio.addEventListener("timeupdate", timeUpdateHandler);
      });
    } else {
      audio.pause();
      audio.removeEventListener("timeupdate", timeUpdateHandler);
    }

    return () => {
      audio.pause();
      audio.removeEventListener("timeupdate", timeUpdateHandler);
    };
  }, [playing, playList, setPlaying]);

페이지를 벗어나면 음악이 정지 됩니다.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant