Skip to content

Commit

Permalink
Merge pull request #24 from LacLic/master
Browse files Browse the repository at this point in the history
  • Loading branch information
Yesterday17 authored Mar 13, 2024
2 parents a82991b + 6b1ca8c commit 98f70b5
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 11 deletions.
8 changes: 5 additions & 3 deletions src/hooks/usePlayer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useCallback } from "react";
import { useRecoilState, useRecoilValue, useSetRecoilState } from "recoil";
import { NowPlayingInfoState, PlayerState, PlayerStatusState } from "@/state/player";
import { NowPlayingInfoState, PlayerDurationState, PlayerState, PlayerStatusState } from "@/state/player";
import { PlayerStatus, PlayQueueItem } from "@/types/common";
import { addQueries } from "@/utils/url";
import useLocalStorageValue from "./useLocalStorageValue";
Expand Down Expand Up @@ -50,7 +50,7 @@ async function getAudioPlayInfo(url: string, quality?: string): Promise<AudioPla
}

if (!MediaSource.isTypeSupported(mime)) {
resolveAudioPlayInfo({ url, useMSE: false });
resolveAudioPlayInfo({ url, useMSE: false, duration: +duration });
return;
}

Expand Down Expand Up @@ -100,6 +100,7 @@ async function getAudioPlayInfo(url: string, quality?: string): Promise<AudioPla

export default function usePlayer() {
const player = useRecoilValue(PlayerState);
const [playerDuration, setPlayerDuration] = useRecoilState(PlayerDurationState);
const [quality] = useLocalStorageValue("player.quality", "lossless");
const [playerStatus, setPlayerStatus] = useRecoilState(PlayerStatusState);
const setNowPlayingInfo = useSetRecoilState(NowPlayingInfoState);
Expand All @@ -119,14 +120,15 @@ export default function usePlayer() {
return;
}
setPlayerStatus(PlayerStatus.BUFFERING);
// TODO: make use of audioInfo.useMSE and audioInfo.duration
// TODO: make use of audioInfo.useMSE
const audioInfo = await getAudioPlayInfo(playUrl, quality);
player.src = audioInfo.url;
player.addEventListener(
"canplay",
() => {
player.play();
setPlayerStatus(PlayerStatus.PLAYING);
if(!!audioInfo.duration) setPlayerDuration(+audioInfo.duration);
setNowPlayingInfo({
title,
artist,
Expand Down
13 changes: 5 additions & 8 deletions src/hooks/usePlayerTime.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
import { useEffect, useState } from "react";
import usePlayer from "./usePlayer";
import { useRecoilValue } from "recoil";
import { PlayerDurationState } from "@/state/player";

export default function usePlayerTime() {
const [player] = usePlayer();
const [currentTime, setCurrentTime] = useState(0);
const [duration, setDuration] = useState(0);
const duration = useRecoilValue(PlayerDurationState);
const [player] = usePlayer();
useEffect(() => {
const onTimeUpdate = () => {
setCurrentTime(player.currentTime);
};
const onDurationChange = () => {
setDuration(player.duration);
};
player.addEventListener("timeupdate", onTimeUpdate);
player.addEventListener("durationchange", onDurationChange);
return () => {
player.removeEventListener("timeupdate", onTimeUpdate);
player.removeEventListener("durationchange", onDurationChange);
};
}, [player]);
}, [player, duration]);
return [currentTime, duration] as const;
}
5 changes: 5 additions & 0 deletions src/state/player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,8 @@ export const NowPlayingInfoState = atom<Partial<PlayQueueItem>>({
key: "NowPlayingInfoState",
default: {},
});

export const PlayerDurationState = atom({
key: "PlayerDurationState",
default: 0,
});

0 comments on commit 98f70b5

Please sign in to comment.