i have this '''import { useCallback, useRef, useState } from 'react'; import {…
- User
i have this '''import { useCallback, useRef, useState } from 'react'; import { AudioConfig } from '../types';
const DEFAULT_AUDIO_CONFIG: AudioConfig = { echoCancellation: true, noiseSuppression: true, sampleRate: 44100, };
export function useAudioRecorder(onAudioChunk: (blob: Blob) => Promise) { const [isRecording, setIsRecording] = useState(false); const mediaRecorderRef = useRef<MediaRecorder | null>(null); const streamRef = useRef<MediaStream | null>(null);
const startRecording = useCallback(async () => { try { const stream = await navigator.mediaDevices.getDisplayMedia({ video: true, audio: DEFAULT_AUDIO_CONFIG, });
codeconst audioTrack = stream.getAudioTracks()[0]; if (!audioTrack) { throw new Error('No audio track found in the shared content'); } const audioStream = new MediaStream([audioTrack]); streamRef.current = stream; const mediaRecorder = new MediaRecorder(audioStream, { mimeType: 'audio/webm', }); mediaRecorderRef.current = mediaRecorder; mediaRecorder.ondataavailable = async (e) => { if (e.data.size > 0) { await onAudioChunk(e.data); } }; mediaRecorder.start(3000); setIsRecording(true); } catch (error) { console.error('Error starting recording:', error); throw new Error('Failed to start recording'); }}, [onAudioChunk]);
const stopRecording = useCallback(() => { if (mediaRecorderRef.current && isRecording) { mediaRecorderRef.current.stop(); if (streamRef.current) { streamRef.current.getTracks().forEach(track => track.stop()); } setIsRecording(false); } }, [isRecording]);
return { isRecording, startRecording, stopRecording, }; }''' i want to find a js libraryr can handle chunking audio from screenshare navigator.mediaDevices.getDisplayMedia . because the above script first chunck audio is good the others after it doesn't not work
- Assistant