|
|
|
@ -14,25 +14,26 @@ import PlayerControl from './PlayerControl';
|
|
|
|
|
import useAudioStore from '@/store/audio';
|
|
|
|
|
|
|
|
|
|
export default function AudioPlayer({ className }: { className?: string }) {
|
|
|
|
|
const audioRef = useRef(new Audio());
|
|
|
|
|
const [trackProgress, setTrackProgress] = useState<number>(0); // 音频进度
|
|
|
|
|
const isReady = useRef<boolean>(false); // 是否完成加载
|
|
|
|
|
|
|
|
|
|
const { duration } = audioRef.current; // 音频总时长;
|
|
|
|
|
|
|
|
|
|
const { playStatus, setPlayStatus, audio, switchSongByDiff } = useAudioStore(
|
|
|
|
|
useShallow((state) => ({
|
|
|
|
|
playStatus: state.playStatus,
|
|
|
|
|
setPlayStatus: state.setPlayStatus,
|
|
|
|
|
audio: state.audio,
|
|
|
|
|
playList: state.playList,
|
|
|
|
|
switchSongByDiff: state.switchSongByDiff,
|
|
|
|
|
})),
|
|
|
|
|
const { audio, switchSongByDiff } = useAudioStore(
|
|
|
|
|
useShallow((state) => {
|
|
|
|
|
return {
|
|
|
|
|
audio: state.audio,
|
|
|
|
|
playList: state.playList,
|
|
|
|
|
switchSongByDiff: state.switchSongByDiff,
|
|
|
|
|
};
|
|
|
|
|
}),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// const musicPlayers = useRef<HTMLAudioElement | undefined>(typeof Audio !== 'undefined' ? new Audio('') : undefined);
|
|
|
|
|
const audioRef = useRef(new Audio(''));
|
|
|
|
|
const [isPlaying, setIsPlaying] = useState(false); // 播放状态
|
|
|
|
|
const [trackProgress, setTrackProgress] = useState<number>(0); // 音频进度(s)
|
|
|
|
|
const isFirst = useRef<boolean>(false); // 第一次进入
|
|
|
|
|
const { duration } = audioRef.current; // 音频总时长(s)
|
|
|
|
|
|
|
|
|
|
// 播放/暂停
|
|
|
|
|
const handlePlay = () => {
|
|
|
|
|
setPlayStatus(!playStatus);
|
|
|
|
|
setIsPlaying(!isPlaying);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// 上一首
|
|
|
|
@ -45,47 +46,47 @@ export default function AudioPlayer({ className }: { className?: string }) {
|
|
|
|
|
switchSongByDiff(1);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// 调整进度
|
|
|
|
|
// 调整播放进度
|
|
|
|
|
const handleChangeProgress = (value: string) => {
|
|
|
|
|
audioRef.current.currentTime = Number(value);
|
|
|
|
|
setTrackProgress(audioRef.current.currentTime);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// 给 audio 绑定监听
|
|
|
|
|
// 监听音频进度
|
|
|
|
|
const onTimeUpdate = () => {
|
|
|
|
|
if (audioRef.current.ended) handlePlayNext();
|
|
|
|
|
setTrackProgress(audioRef.current.currentTime);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// 给 audio 绑定 timeupdate 监听
|
|
|
|
|
const handleTimeUpdate = (status: boolean) => {
|
|
|
|
|
status
|
|
|
|
|
? audioRef.current.addEventListener('timeupdate', onTimeUpdate)
|
|
|
|
|
: audioRef.current.removeEventListener('timeupdate', onTimeUpdate);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// 监听进度
|
|
|
|
|
const onTimeUpdate = () => {
|
|
|
|
|
if (audioRef.current.ended) handlePlayNext();
|
|
|
|
|
setTrackProgress(audioRef.current.currentTime);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// 监听播放/暂停
|
|
|
|
|
// 播放/暂停
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
playStatus ? audioRef.current.play() : audioRef.current.pause();
|
|
|
|
|
}, [playStatus]);
|
|
|
|
|
isPlaying ? audioRef.current.play() : audioRef.current.pause();
|
|
|
|
|
}, [isPlaying]);
|
|
|
|
|
|
|
|
|
|
// 监听切换歌曲
|
|
|
|
|
// 切换歌曲
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
console.log('触发');
|
|
|
|
|
handleTimeUpdate(false);
|
|
|
|
|
audioRef.current.pause();
|
|
|
|
|
if (!audio) return;
|
|
|
|
|
audioRef.current = new Audio(audio.src);
|
|
|
|
|
|
|
|
|
|
if (isReady.current) {
|
|
|
|
|
setTrackProgress(audioRef.current.currentTime);
|
|
|
|
|
console.log('id变化', { audio, isFirst: isFirst.current });
|
|
|
|
|
if (isFirst.current) {
|
|
|
|
|
audioRef.current.play();
|
|
|
|
|
setPlayStatus(true);
|
|
|
|
|
setIsPlaying(true);
|
|
|
|
|
handleTimeUpdate(true);
|
|
|
|
|
} else {
|
|
|
|
|
isReady.current = true;
|
|
|
|
|
isFirst.current = true;
|
|
|
|
|
}
|
|
|
|
|
}, [audio]);
|
|
|
|
|
|
|
|
|
|
// 卸载时的处理
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
return () => {
|
|
|
|
|
audioRef.current.pause();
|
|
|
|
@ -96,7 +97,7 @@ export default function AudioPlayer({ className }: { className?: string }) {
|
|
|
|
|
return (
|
|
|
|
|
<div>
|
|
|
|
|
<PlayerControl
|
|
|
|
|
playStatus={playStatus}
|
|
|
|
|
playStatus={isPlaying}
|
|
|
|
|
audio={audio}
|
|
|
|
|
duration={duration}
|
|
|
|
|
onPlay={handlePlay}
|
|
|
|
|