diff --git a/src/components/speech-content/audio-player.tsx b/src/components/speech-content/audio-player.tsx index 685c37c0..cef16354 100644 --- a/src/components/speech-content/audio-player.tsx +++ b/src/components/speech-content/audio-player.tsx @@ -13,24 +13,35 @@ interface AudioPlayerProps { ref?: any; height?: number; width?: number; + onReady?: () => void; + onClick?: (value: number) => void; } const AudioPlayer: React.FC = forwardRef((props, ref) => { const { autoplay, audioUrl, speed = 1, ...rest } = props; const container = useRef(null); - const { createWavesurfer, play, pause, destroyWavesurfer, wavesurfer } = - useWavesurfer({ - container, - autoplay: autoplay, - url: audioUrl, - audioRate: speed, - ...rest - }); + const { + createWavesurfer, + play, + pause, + duration, + destroyWavesurfer, + wavesurfer + } = useWavesurfer({ + container, + autoplay: autoplay, + url: audioUrl, + audioRate: speed, + onReady: props.onReady, + onClick: props.onClick, + ...rest + }); useImperativeHandle(ref, () => { return { play, - pause + pause, + duration }; }); diff --git a/src/components/speech-content/hooks/use-wavesurfer.ts b/src/components/speech-content/hooks/use-wavesurfer.ts index 90ba007b..1519e942 100644 --- a/src/components/speech-content/hooks/use-wavesurfer.ts +++ b/src/components/speech-content/hooks/use-wavesurfer.ts @@ -11,6 +11,8 @@ interface Options { barRadius?: number; autoplay?: boolean; audioRate?: number; + onReady?: () => void; + onClick: (value: any) => void; } const useWavesurfer = (options: Options) => { const wavesurfer = useRef(null); @@ -37,6 +39,13 @@ const useWavesurfer = (options: Options) => { cursorWidth: 0, ...rest }); + wavesurfer.current?.on('ready', () => { + options.onReady?.(); + }); + + wavesurfer.current?.on('click', (value) => { + options.onClick?.(value); + }); }; const destroyWavesurfer = () => { @@ -51,6 +60,13 @@ const useWavesurfer = (options: Options) => { } }; + const duration = () => { + if (wavesurfer.current) { + return wavesurfer.current.getDuration(); + } + return 0; + }; + const pause = () => { if (wavesurfer.current) { wavesurfer.current.pause(); @@ -62,6 +78,7 @@ const useWavesurfer = (options: Options) => { play, pause, wavesurfer, + duration, destroyWavesurfer }; }; diff --git a/src/components/speech-content/ih.mp4 b/src/components/speech-content/ih.mp4 deleted file mode 100644 index 7e684ebd..00000000 Binary files a/src/components/speech-content/ih.mp4 and /dev/null differ diff --git a/src/components/speech-content/speech-item.tsx b/src/components/speech-content/speech-item.tsx index 35e3021e..7284f894 100644 --- a/src/components/speech-content/speech-item.tsx +++ b/src/components/speech-content/speech-item.tsx @@ -1,4 +1,5 @@ import IconFont from '@/components/icon-font'; +import { formatTime } from '@/utils/index'; import { DownloadOutlined, PauseCircleOutlined, @@ -37,7 +38,9 @@ const SpeechItem: React.FC = (props) => { const intl = useIntl(); const [collapsed, setCollapsed] = useState(false); const [isPlay, setIsPlay] = useState(false); - const ref = useRef(null); + const [duration, setDuration] = useState(0); + const [currentTime, setCurrentTime] = useState(0); + const ref = useRef(null); const handlePlay = () => { if (isPlay) { @@ -53,6 +56,15 @@ const SpeechItem: React.FC = (props) => { setCollapsed(!collapsed); }; + const handleReay = () => { + const duration = ref.current?.duration?.(); + setDuration(duration < 1 ? 1 : duration); + }; + + const handleOnClick = (value: number) => { + console.log('current:', value); + }; + const onDownload = () => { const url = props.audioUrl || ''; const filename = `audio-${dayjs().format('YYYYMMDDHHmmss')}.${props.format}`; @@ -75,6 +87,8 @@ const SpeechItem: React.FC = (props) => { @@ -82,9 +96,8 @@ const SpeechItem: React.FC = (props) => {
{props.format} - {/* - {props.speed}x */} + {formatTime(duration)}
= (props) => { } >