Skip to content

Commit

Permalink
馃悰 fix: Fix Audio in ssr
Browse files Browse the repository at this point in the history
  • Loading branch information
canisminor1990 committed Dec 4, 2023
1 parent f5b3fbf commit 2e5e4e5
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 8 deletions.
1 change: 0 additions & 1 deletion src/core/const/polyfill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ const getSpeechSynthesisUtterance = () => {
);
} catch {}
};

export const SpeechRecognition = getSpeechRecognition();
export const SpeechSynthesis = getSpeechSynthesis();
export const SpeechSynthesisUtterance = getSpeechSynthesisUtterance();
8 changes: 5 additions & 3 deletions src/react/hooks/useBlobUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ export const useBlobUrl = (src: string) => {
if (audio) audio.remove();
if (url) URL.revokeObjectURL(url);
setBlob(blob);
const newAudio = playAudioBlob(blob);
setUrl(newAudio.url);
setAudio(newAudio.audio);
try {
const newAudio = playAudioBlob(blob);
setUrl(newAudio.url);
setAudio(newAudio.audio);
} catch {}
setIsGlobalLoading(false);
},
revalidateOnFocus: false,
Expand Down
22 changes: 18 additions & 4 deletions src/react/hooks/useStreamAudioPlayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,33 @@ export interface StreamAudioPlayerReturn extends AudioProps {
}

export const useStreamAudioPlayer = (): StreamAudioPlayerReturn => {
const audioRef = useRef<HTMLAudioElement>(new Audio());
const audioRef = useRef<HTMLAudioElement>();
const [arrayBuffers, setArrayBuffers] = useState<ArrayBuffer[]>([]);
const [currentTime, setCurrentTime] = useState(0);
const [duration, setDuration] = useState(0);
const [isPlaying, setIsPlaying] = useState(false);
const [maxLength, setMaxLength] = useState(0);

useEffect(() => {
try {
audioRef.current = new Audio();
} catch {}

if (!audioRef.current) return;
const onLoadedMetadata = () => {
if (!audioRef.current) return;
setDuration(audioRef.current.duration);
};
const onTimeUpdate = () => {
if (!audioRef.current) return;
setCurrentTime(audioRef.current.currentTime);
};

audioRef.current.addEventListener('loadedmetadata', onLoadedMetadata);
audioRef.current.addEventListener('timeupdate', onTimeUpdate);

return () => {
if (!audioRef.current) return;
audioRef.current.pause();
audioRef.current.load();
audioRef.current.removeEventListener('loadedmetadata', onLoadedMetadata);
Expand Down Expand Up @@ -64,13 +71,14 @@ export const useStreamAudioPlayer = (): StreamAudioPlayerReturn => {
audioRef.current.addEventListener('ended', onEnded);

return () => {
if (!audioRef.current) return;
audioRef.current.removeEventListener('ended', onEnded);
};
}, [maxLength, arrayBuffers]);

const loadArrayBuffer = useCallback(
async (arrayBuffer: ArrayBuffer) => {
if (!arrayBuffer) return;
if (!arrayBuffer || !audioRef.current) return;
if (maxLength === 0) {
const newBlob = new Blob([arrayBuffer], { type: 'audio/mp3' });
audioRef.current.src = URL.createObjectURL(newBlob);
Expand All @@ -85,29 +93,34 @@ export const useStreamAudioPlayer = (): StreamAudioPlayerReturn => {
);

const handlePlay = useCallback(() => {
if (!audioRef.current) return;
if (audioRef.current.duration > 0) {
setIsPlaying(true);
audioRef.current.play();
}
}, []);

const handlePause = useCallback(() => {
if (!audioRef.current) return;
setIsPlaying(false);
audioRef.current.pause();
}, []);

const handleStop = useCallback(() => {
if (!audioRef.current) return;
setIsPlaying(false);
audioRef.current.pause();
audioRef.current.currentTime = 0;
}, []);

const setTime = useCallback((value: number) => {
if (!audioRef.current) return;
setCurrentTime(value);
audioRef.current.currentTime = value;
}, []);

const reset = useCallback(() => {
if (!audioRef.current) return;
audioRef.current.pause();
audioRef.current.currentTime = 0;
if (audioRef.current.src) URL.revokeObjectURL(audioRef.current.src);
Expand All @@ -119,6 +132,7 @@ export const useStreamAudioPlayer = (): StreamAudioPlayerReturn => {
}, []);

const handleDownload = useCallback(async () => {
if (!audioRef.current) return;
const a = document.createElement('a');
a.href = audioRef.current.src;
a.download = 'audio.mp3';
Expand All @@ -134,10 +148,10 @@ export const useStreamAudioPlayer = (): StreamAudioPlayerReturn => {
load: loadArrayBuffer,
pause: handlePause,
play: handlePlay,
ref: audioRef,
ref: audioRef as any,
reset,
setTime,
stop: handleStop,
url: audioRef.current.src,
url: audioRef?.current?.src || '',
};
};

0 comments on commit 2e5e4e5

Please sign in to comment.