From 97ef59bd68d87c3ab03bc57b87dbd2f0db01eb6b Mon Sep 17 00:00:00 2001 From: Maria Molchanova Date: Sun, 30 Mar 2025 00:43:03 -0400 Subject: [PATCH] safari record for call screen --- React/src/app/call/page.tsx | 101 +++++++++++++++++++++++++++++++----- 1 file changed, 88 insertions(+), 13 deletions(-) diff --git a/React/src/app/call/page.tsx b/React/src/app/call/page.tsx index 4eb2755..5fdfead 100644 --- a/React/src/app/call/page.tsx +++ b/React/src/app/call/page.tsx @@ -1,30 +1,105 @@ 'use client'; -import React from 'react'; +import React, { useState, useRef } from 'react'; -const CallPage = () => { +function CallPage() { const [callDuration, setCallDuration] = React.useState(0); + const [isRecording, setIsRecording] = useState(false); + const [audioUrl, setAudioUrl] = useState(null); + const [audioBlob, setAudioBlob] = useState(null); - React.useEffect(() => { - const interval = setInterval(() => { - setCallDuration((prev) => prev + 1); - }, 1000); + const mediaRecorderRef = useRef(null); // Reference for the MediaRecorder instance + const audioChunks = useRef([]); // Array to store audio data chunks + + /* + React.useEffect(() => { + const interval = setInterval(() => { + setCallDuration((prev) => prev + 1); + }, 1000); + + return () => clearInterval(interval); + }, []); + */ + + // Start recording + const startRecording = async () => { + try { + console.log("Start recording function called"); + // permissions + const stream = await navigator.mediaDevices.getUserMedia({ audio: true }); + mediaRecorderRef.current = new MediaRecorder(stream); + // Collect audio data in chunks + mediaRecorderRef.current.ondataavailable = (event) => { + console.log("ondataavailable triggered, chunk size:", event.data.size); + audioChunks.current.push(event.data); + + }; + + // Handle stop event + mediaRecorderRef.current.onstop = () => { + const audioBlob = new Blob(audioChunks.current, { type: 'audio/mp4' }); + console.log("Total chunks received:", audioChunks.current.length); + const audioUrl = URL.createObjectURL(audioBlob); + setAudioBlob(audioBlob); + console.log('media recorder type', mediaRecorderRef.current.mimeType) + console.log(audioBlob); + setAudioUrl(audioUrl); + audioChunks.current = []; + }; + + mediaRecorderRef.current.start(1000); + console.log("Recording started"); + setIsRecording(true); + } catch (err) { + console.error("Error accessing microphone:", err); + } + }; + + // Stop recording + const stopRecording = () => { + if (mediaRecorderRef.current && mediaRecorderRef.current.state !== "inactive") { + mediaRecorderRef.current.stop(); + setIsRecording(false); + } + }; + + // Play the recorded audio + const playAudio = () => { + const audio = new Audio(audioUrl); + audio.play(); + }; - return () => clearInterval(interval); - }, []); return (
+ +

- Call with a Definitely Real Person

+ Call with a Definitely Real Person + +
{callDuration}s
+ + {isRecording ? ( + + ) : ( + + )} + {audioUrl && ( + <> +

Recorded Audio

+ + + + )} +