import { FileTextFilled } from '@ant-design/icons'; import { useRef, useState } from 'react'; import { IChatItem } from '../ChatLogsType'; export const ChatVoice: React.FC = (props) => { const [open, setOpen] = useState(false); const audioRef = useRef(); function readBlob(blob: Blob, callback: Function) { const reader = new FileReader(); reader.onload = function (e) { const data = new Uint8Array(e.target.result); callback(data); }; reader.readAsArrayBuffer(blob); } function content() { try { const msg = JSON.parse(props.chat?.content as string); return (
{ const audios = document.querySelectorAll('audio'); if (audios) { audios.forEach((el) => { el.pause(); }); } if (!open) { fetch(`/api/${msg.path}`) .then((response) => response.blob()) .then((res) => { let blob = new Blob([res]); readBlob(blob, function (data: any) { if (window.AMR) { var buffer = window.AMR.toWAV(data); var url = URL.createObjectURL(new Blob([buffer], { type: 'audio/x-wav' })); audioRef.current.src = url; audioRef.current.play().catch((err: any) => { console.log(err); }); } }); }); setOpen(true); } else { audioRef.current.play().catch((err: any) => { console.log(err); }); } }} > {msg.play_length}"
); } catch (_e) { return (
{props.chat?.content}
); } } return <>{content()}; };