Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: audio player pause #222

Merged
merged 1 commit into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/components/audio-animation/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ interface AudioAnimationProps {
maxWidth?: number;
scaleFactor?: number;
maxBarCount?: number;
amplitude?: number;
fixedHeight?: boolean;
analyserData: {
data: Uint8Array;
Expand All @@ -19,6 +20,7 @@ const AudioAnimation: React.FC<AudioAnimationProps> = (props) => {
const {
scaleFactor = 1.2,
maxBarCount = 128,
amplitude = 40,
maxWidth,
fixedHeight = true,
analyserData,
Expand Down Expand Up @@ -71,7 +73,7 @@ const AudioAnimation: React.FC<AudioAnimationProps> = (props) => {
const barSpacing = 6;
const centerLine = Math.floor(HEIGHT / 2);

const jitterAmplitude = 40;
const jitterAmplitude = amplitude;
const minJitter = 10;

let lastFrameTime = 0;
Expand Down
39 changes: 39 additions & 0 deletions src/components/audio-player/config/type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
export type AudioEvent =
| 'play'
| 'playing'
| 'pause'
| 'timeupdate'
| 'ended'
| 'loadedmetadata'
| 'audioprocess'
| 'canplay'
| 'ended'
| 'loadeddata'
| 'seeked'
| 'seeking'
| 'volumechange';

export interface AudioPlayerProps {
controls?: boolean;
autoplay?: boolean;
url: string;
speed?: number;
ref?: any;
height?: number;
width?: number;
duration?: number;
onPlay?: () => void;
onPlaying?: () => void;
onPause?: () => void;
onTimeUpdate?: () => void;
onEnded?: () => void;
onLoadedMetadata?: (duration: number) => void;
onAudioProcess?: (current: number) => void;
onCanPlay?: () => void;
onLoadedData?: () => void;
onSeeked?: () => void;
onSeeking?: () => void;
onVolumeChange?: () => void;
onReady?: (duration: number) => void;
onAnalyse?: (analyseData: any, frequencyBinCount: any) => void;
}
1 change: 1 addition & 0 deletions src/components/audio-player/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ const AudioPlayer: React.FC<AudioPlayerProps> = forwardRef((props, ref) => {
}, []);

const handleAudioOnPlay = useCallback(() => {
console.log('audio play');
timer.current = setInterval(() => {
setAudioState((prestate) => {
return {
Expand Down
164 changes: 164 additions & 0 deletions src/components/audio-player/raw-audio-player.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
import React, {
forwardRef,
useCallback,
useEffect,
useImperativeHandle,
useRef
} from 'react';
import { AudioPlayerProps } from './config/type';

const RawAudioPlayer: React.FC<AudioPlayerProps> = forwardRef((props, ref) => {
const { autoplay = false } = props;
const audioRef = React.useRef<HTMLAudioElement>(null);

// =================== audio context ======================
const audioContext = useRef<any>(null);
const analyser = useRef<any>(null);
const dataArray = useRef<any>(null);
// ========================================================

const initAudioContext = useCallback(() => {
audioContext.current = new (window.AudioContext ||
window.webkitAudioContext)();

analyser.current = audioContext.current.createAnalyser();
analyser.current.fftSize = 512;
dataArray.current = new Uint8Array(analyser.current.frequencyBinCount);
}, []);

const generateVisualData = useCallback(() => {
const source = audioContext.current.createMediaElementSource(
audioRef.current
);
source.connect(analyser.current);
analyser.current.connect(audioContext.current.destination);
}, []);

const initEnvents = () => {
if (!audioRef.current) {
return;
}

audioRef.current.addEventListener('complete', () => {});

audioRef.current.addEventListener('play', () => {
props.onAnalyse?.(dataArray.current, analyser);
props.onPlay?.();
});

audioRef.current.addEventListener('pause', () => {
props.onAnalyse?.(dataArray.current, analyser);
props.onPause?.();
});

audioRef.current.addEventListener('timeupdate', () => {
props.onTimeUpdate?.();
});

audioRef.current.addEventListener('ended', () => {
props.onEnded?.();
});
// add all other events

audioRef.current.addEventListener('canplay', () => {
props.onCanPlay?.();
});

audioRef.current.addEventListener('loadeddata', () => {
initEnvents();
initAudioContext();
generateVisualData();
props.onLoadedData?.();
});

audioRef.current.addEventListener('seeked', () => {
props.onSeeked?.();
});

audioRef.current.addEventListener('seeking', () => {
props.onSeeking?.();
});

audioRef.current.addEventListener('volumechange', () => {
props.onVolumeChange?.();
});

audioRef.current.addEventListener('audioprocess', () => {
const current = audioRef.current?.currentTime || 0;
props.onAudioProcess?.(current);
});

audioRef.current.addEventListener('playing', () => {
props.onPlaying?.();
});

audioRef.current.addEventListener('loadedmetadata', () => {
const duration = audioRef.current?.duration || 0;
props.onLoadedMetadata?.(duration);
props.onReady?.(duration);
});

audioRef.current.addEventListener('ended', () => {
props.onEnded?.();
});

audioRef.current.addEventListener('loadeddata', () => {
props.onLoadedData?.();
});
};

const handleAudioOnPlay = () => {
audioRef.current?.play();
};

const handleLoadedMetadata = () => {};

useImperativeHandle(ref, () => ({
play: () => {
audioRef.current?.play();
},
pause: () => {
audioRef.current?.pause();
}
}));

useEffect(() => {
if (audioRef.current) {
}
return () => {
if (audioContext.current) {
audioContext.current.close();
}
// remove all events

audioRef.current?.removeEventListener('play', () => {});
audioRef.current?.removeEventListener('pause', () => {});
audioRef.current?.removeEventListener('timeupdate', () => {});
audioRef.current?.removeEventListener('ended', () => {});
audioRef.current?.removeEventListener('canplay', () => {});
audioRef.current?.removeEventListener('loadeddata', () => {});
audioRef.current?.removeEventListener('seeked', () => {});
audioRef.current?.removeEventListener('seeking', () => {});
audioRef.current?.removeEventListener('volumechange', () => {});
audioRef.current?.removeEventListener('audioprocess', () => {});
audioRef.current?.removeEventListener('playing', () => {});
audioRef.current?.removeEventListener('loadedmetadata', () => {});
audioRef.current?.removeEventListener('ended', () => {});
audioRef.current?.removeEventListener('loadeddata', () => {});
};
}, [audioRef.current]);

return (
<audio
width={props.width || 300}
height={props.height || 40}
controls
autoPlay={autoplay}
src={props.url}
ref={audioRef}
preload="metadata"
></audio>
);
});

export default RawAudioPlayer;
17 changes: 8 additions & 9 deletions src/components/speech-content/speech-item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,6 @@ const SpeechItem: React.FC<SpeechContentProps> = (props) => {

const handlePlay = useCallback(async () => {
try {
console.log(
'isPlay:',
isPlay,
ref.current?.wavesurfer.current?.isPlaying()
);
ref.current?.pause();
if (ref.current?.wavesurfer.current?.isPlaying()) {
ref.current?.pause();
setIsPlay(false);
Expand Down Expand Up @@ -124,6 +118,11 @@ const SpeechItem: React.FC<SpeechContentProps> = (props) => {
debounceSeek(value);
};

const handleReady = useCallback((duration: number) => {
console.log('duration:', duration);
setDuration(duration);
}, []);

const handlOnChangeComplete = useCallback((value: number) => {
ref.current?.seekTo(value / duration);
setCurrentTime(value);
Expand Down Expand Up @@ -152,8 +151,7 @@ const SpeechItem: React.FC<SpeechContentProps> = (props) => {
<AudioPlayer
{...props}
audioUrl={props.audioUrl}
onReady={handleReay}
onClick={handleOnClick}
onReady={handleReady}
onFinish={handleOnFinish}
onPlay={handleOnPlay}
onPause={handleOnPause}
Expand All @@ -163,7 +161,8 @@ const SpeechItem: React.FC<SpeechContentProps> = (props) => {
></AudioPlayer>
{isPlay && (
<AudioAnimation
maxBarCount={180}
maxBarCount={100}
amplitude={60}
fixedHeight={true}
height={82}
width={800}
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/use-chunk-fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const useSetChunkFetch = () => {
}

const chunk = decoder.decode(value, { stream: true });

console.log('chunk===', chunk);
callback(chunk);
// console.log('chunkDataRef.current===2', chunkDataRef.current);

Expand Down
8 changes: 4 additions & 4 deletions src/pages/playground/components/ground-images.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -419,14 +419,14 @@ const GroundImages: React.FC<MessageProps> = forwardRef((props, ref) => {
useEffect(() => {
if (size === 'custom') {
form.current?.form?.setFieldsValue({
width: 256,
height: 256
width: 512,
height: 512
});
setParams((pre: object) => {
return {
...pre,
width: 256,
height: 256
width: 512,
height: 512
};
});
}
Expand Down
8 changes: 3 additions & 5 deletions src/pages/playground/config/params-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,7 @@ export const ImageParamsConfig: ParamsSchema[] = [
{ label: 'playground.params.custom', value: 'custom', locale: true },
{ label: '256x256', value: '256x256' },
{ label: '512x512', value: '512x512' },
{ label: '1024x1024', value: '1024x1024' },
{ label: '1792x1024', value: '1792x1024' },
{ label: '1024x1792', value: '1024x1792' }
{ label: '1024x1024', value: '1024x1024' }
],
label: {
text: 'playground.params.size',
Expand Down Expand Up @@ -367,7 +365,7 @@ export const ImageCustomSizeConfig: ParamsSchema[] = [
},
attrs: {
min: 256,
max: 1792,
max: 1024,
step: 64,
inputnumber: false
},
Expand All @@ -387,7 +385,7 @@ export const ImageCustomSizeConfig: ParamsSchema[] = [
},
attrs: {
min: 256,
max: 1792,
max: 1024,
step: 64,
inputnumber: false
},
Expand Down