From b2fd82e9f40777404d7e8d3b1b132f268029ba91 Mon Sep 17 00:00:00 2001 From: amass <168062547@qq.com> Date: Wed, 14 Jun 2023 15:06:52 +0800 Subject: [PATCH] =?UTF-8?q?1.=E5=AE=8C=E6=88=90=E8=BF=9B=E5=BA=A6=E6=9D=A1?= =?UTF-8?q?=E7=9A=84seek=E5=8A=9F=E8=83=BD=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/MainPage.js | 5 ++-- src/PlayerBar.js | 27 ++++++++++------- src/RecordLyrics.js | 10 +++++-- src/business/recorderSlice.js | 6 +++- src/components/Waveform.js | 43 ++++++++++++--------------- src/components/useSetTrackProgress.js | 32 -------------------- 6 files changed, 50 insertions(+), 73 deletions(-) delete mode 100644 src/components/useSetTrackProgress.js diff --git a/src/MainPage.js b/src/MainPage.js index 2be11b0..e88d785 100644 --- a/src/MainPage.js +++ b/src/MainPage.js @@ -35,6 +35,7 @@ export default function () { const dispatch = useDispatch() const accessToken = useSelector(state => state.user.accessToken); const passportId = useSelector(state => state.user.passportId); + const currentTime = useSelector(state => state.recorder.currentTime); useEffect(() => { yzs.get_record_list(accessToken, passportId).then(list => { dispatch(setList(list.result)); @@ -53,8 +54,8 @@ export default function () { mr: `24px`, }} > - - + + diff --git a/src/PlayerBar.js b/src/PlayerBar.js index 321ea35..95a043f 100644 --- a/src/PlayerBar.js +++ b/src/PlayerBar.js @@ -6,7 +6,7 @@ import { useResizeDetector } from 'react-resize-detector'; import pauseIcon from "./assets/play.png"; import playIcon from "./assets/pause.png"; import downloadIcon from "./assets/download.png"; -import { togglePauseState } from "./business/recorderSlice.js" +import { setCurrentTime, togglePauseState } from "./business/recorderSlice.js" import Waveform from "./components/Waveform"; const durationFormat = (time) => { @@ -18,10 +18,9 @@ const durationFormat = (time) => { return hour.toString().padStart(2, '0') + ":" + minute.toString().padStart(2, '0') + ":" + second.toString().padStart(2, '0'); } -export default function () { +export default function ({ currentTime }) { const dispatch = useDispatch(); - const [duration, setDuration] = useState("00:00:00"); - const [currentTime, setCurrentTime] = useState("00:00:00"); + const [duration, setDuration] = useState(0); const [canvasWidth, setCanvasWidth] = useState(0); const currentIndex = useSelector(state => state.recorder.currentIndex); const recordList = useSelector(state => state.recorder.list); @@ -43,10 +42,10 @@ export default function () { }; const onDurationChange = (event) => { - setDuration(durationFormat(player.current.duration)); + setDuration(player.current.duration); } const onTimeUpdate = (event) => { - setCurrentTime(durationFormat(player.current.currentTime)); + dispatch(setCurrentTime(player.current.currentTime)); } const onResize = useCallback((width, height) => { @@ -57,7 +56,15 @@ export default function () { onResize: onResize }); - return + const seekRecord = (second) => { + player.current.currentTime = second; + } + + return diff --git a/src/RecordLyrics.js b/src/RecordLyrics.js index e42ac31..94f7c8a 100644 --- a/src/RecordLyrics.js +++ b/src/RecordLyrics.js @@ -3,9 +3,14 @@ import { Typography, Paper } from "@mui/material"; import styles from './RecordLyrics.module.css'; import { useSelector, useDispatch } from 'react-redux' +function isHighlight(currentTime, { start, end }) { + currentTime *= 1000; + return (currentTime > start) && (currentTime <= end); +} -export default function () { + +export default function ({ currentTime }) { const currentLyric = useSelector(state => state.recorder.currentLyric); const currentIndex = useSelector(state => state.recorder.currentIndex); const recordList = useSelector(state => state.recorder.list); @@ -16,8 +21,7 @@ export default function () { return {recordList.at(currentIndex).type === 1 ? (typeof currentLyric === "object" ? currentLyric.map((lyric, index) => { return
- {lyric.text} - {lyric.translation} + {lyric.text}
}) : ) :
{typeof currentLyric === "string" ? currentLyric : ""}
}
diff --git a/src/business/recorderSlice.js b/src/business/recorderSlice.js index b7a0dd1..92a6424 100644 --- a/src/business/recorderSlice.js +++ b/src/business/recorderSlice.js @@ -7,6 +7,7 @@ export const recorderSlice = createSlice({ currentIndex: 0, currentLyric: [], currentBlob: {}, + currentTime: 0, // 当前音频播放时间 pause: true, }, reducers: { @@ -23,6 +24,9 @@ export const recorderSlice = createSlice({ setCurrentBlob: (state, action) => { state.currentBlob = action.payload; }, + setCurrentTime: (state, action) => { + state.currentTime = action.payload; + }, togglePauseState: (state) => { state.pause = !state.pause; }, @@ -30,6 +34,6 @@ export const recorderSlice = createSlice({ }) // Action creators are generated for each case reducer function -export const { setCurrentIndex, setList, setCurrentLyric, setCurrentBlob, togglePauseState } = recorderSlice.actions +export const { setCurrentIndex, setList, setCurrentLyric, setCurrentBlob, togglePauseState, setCurrentTime } = recorderSlice.actions export default recorderSlice.reducer \ No newline at end of file diff --git a/src/components/Waveform.js b/src/components/Waveform.js index 4885d71..17ae108 100644 --- a/src/components/Waveform.js +++ b/src/components/Waveform.js @@ -1,11 +1,10 @@ import { useEffect, useRef, useState, useCallback } from "react"; -import useSetTrackProgress from "./useSetTrackProgress.js" -import { useResizeDetector } from 'react-resize-detector'; const pointWidth = 2; const pointMargin = 3; +const leftPadding = 14; // 留出空间画时间戳 +const rightPadding = 14; -const trackDuration = 60; //