Skip to content

Commit

Permalink
Feat/#337 스와이프 페이지 이동 시 기존 재생 노래 멈춤 기능을 구현한다. (#343)
Browse files Browse the repository at this point in the history
* refactor: 플레이어 전체화면 방지

* feat: 화면 전환시 노래 중단 기능 구현
  • Loading branch information
Creative-Lee authored Aug 18, 2023
1 parent da90a40 commit d38a486
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
25 changes: 23 additions & 2 deletions frontend/src/features/songs/components/KillingPartInterface.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect, useState } from 'react';
import { useEffect, useRef, useState } from 'react';
import { styled } from 'styled-components';
import CommentList from '@/features/comments/components/CommentList';
import useVideoPlayerContext from '@/features/youtube/hooks/useVideoPlayerContext';
Expand All @@ -21,6 +21,7 @@ const KillingPartInterface = ({ killingParts, songId }: KillingPartInterfaceProp
const [isRepeat, setIsRepeat] = useState(false);
const { videoPlayer, playerState, seekTo, pause } = useVideoPlayerContext();
const { countedTime, startTimer, resetTimer, pauseTimer } = useTimerContext();
const observingTargetRef = useRef<HTMLDivElement>(null);

const toggleRepetition = () => {
setIsRepeat((prev) => !prev);
Expand Down Expand Up @@ -98,9 +99,29 @@ const KillingPartInterface = ({ killingParts, songId }: KillingPartInterfaceProp
}
}, [playerState, pauseTimer, resetTimer, startTimer]);

useEffect(() => {
const onObserveTarget: IntersectionObserverCallback = async ([entry], observer) => {
if (entry.intersectionRatio > 0) return;

observer.unobserve(entry.target);
setNowPlayingTrack(DEFAULT_PART_ID);
pause();
observer.observe(entry.target);
};

const observer = new IntersectionObserver(onObserveTarget, {
threshold: [0],
});

if (!observingTargetRef.current) return;
observer.observe(observingTargetRef.current);

return () => observer.disconnect();
}, [pause]);

return (
<>
<FlexContainer>
<FlexContainer ref={observingTargetRef}>
<TitleWrapper aria-label="Top 3 킬링파트 듣기">
<ItalicTitle aria-hidden="true">Top 3</ItalicTitle>
<NormalTitle aria-hidden="true"> Killing part</NormalTitle>
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/features/youtube/components/Youtube.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const Youtube = ({ videoId, start = 0 }: YoutubeProps) => {
videoId,
width: '100%',
height: '100%',
playerVars: { start, rel: 0 },
playerVars: { start, rel: 0, fs: 0 },
events: {
onReady: (e) => {
bindUpdatePlayerStateEvent(e);
Expand Down

0 comments on commit d38a486

Please sign in to comment.