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