Skip to content

Commit

Permalink
#14 feat: 앨범 데이터에 포함된 duration 값을 통해 음악 시간 미리 표시
Browse files Browse the repository at this point in the history
  • Loading branch information
boostksm committed Jul 12, 2023
1 parent 3a2d71e commit 8d68ee4
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 15 deletions.
9 changes: 4 additions & 5 deletions src/components/SongPlayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,7 @@ const SongPlayer = ({
playPrevSong,
playNextSong,
currentTime,
duration,
changeCurrentTime,
updateDuration,
updateCurrentTime,
isPlaying,
isRandom,
Expand All @@ -137,7 +135,6 @@ const SongPlayer = ({
ref={audioRef}
id="player"
loop={loopOptionIdx === 2}
onLoadedMetadata={updateDuration}
onTimeUpdate={updateCurrentTime}
></audio>
<div className="playingSongHeadingBox">
Expand Down Expand Up @@ -194,15 +191,17 @@ const SongPlayer = ({
<input
className="progressBarInput"
type="range"
value={(currentTime / duration) * 100 || 0}
value={
playingSong ? (currentTime / playingSong.duration) * 100 : 0
}
onInput={changeCurrentTime}
aria-label="재생바"
/>
)}
</div>
<div className="playingSongTimeBox">
<div>{timeFormatter.getString(currentTime)}</div>
<div>{timeFormatter.getString(duration)}</div>
<div>{timeFormatter.getString(playingSong?.duration || 0)}</div>
</div>
</div>
</SongPlayerLayout>
Expand Down
15 changes: 5 additions & 10 deletions src/hooks/useSongPlayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ export default function useSongPlayer({
const [isPlaying, setIsPlaying] = useState(false);
const [isRandom, setIsRandom] = useState(false);
const [loopOptionIdx, setLoopOptionIdx] = useState(0);
const [duration, setDuration] = useState(0);
const [currentTime, setCurrentTime] = useState(0);
const audioRef = useRef(null);
const audioSrcManager = useRef(new AudioFetchingManager(10));
Expand Down Expand Up @@ -40,16 +39,16 @@ export default function useSongPlayer({
}, [playingSong]);

useEffect(() => {
console.log(curObjectUrl);
audioRef.current.src = curObjectUrl;
if (isFromHighlight) moveToHighlight();
if (isPlaying) {
audioRef.current.play().catch(console.log);
}
}, [curObjectUrl]);

useEffect(() => {
if (isFromHighlight) moveToHighlight();
}, [isFromHighlight, duration]);
}, [isFromHighlight]);

useEffect(() => {
if (isPlaying) audioRef.current.play().catch(console.log);
Expand Down Expand Up @@ -101,31 +100,27 @@ export default function useSongPlayer({
}, 100);
}, []);

const updateDuration = useCallback((e) => {
setDuration(e.currentTarget.duration);
}, []);
const updateCurrentTime = useCallback((e) => {
const { currentTime } = e.currentTarget;
setCurrentTime(currentTime);
}, []);

const changeCurrentTime = useCallback(
(e) => {
if (!playingSong) return;
const { value } = e.currentTarget;
const nextTime = (value / 100) * duration;
const nextTime = (value / 100) * playingSong.duration;
audioRef.current.currentTime = nextTime;
setCurrentTime(nextTime);
},
[duration]
[playingSong]
);
return {
audioRef,
playPrevSong,
playNextSong,
currentTime,
duration,
changeCurrentTime,
updateDuration,
updateCurrentTime,
isPlaying,
isRandom,
Expand Down

0 comments on commit 8d68ee4

Please sign in to comment.