From 1496c5b78f6a623f4787462b01d9e3dbc3831571 Mon Sep 17 00:00:00 2001 From: albin-karlsson <55614148+albin-karlsson@users.noreply.github.com> Date: Fri, 26 Apr 2024 21:46:42 +0200 Subject: [PATCH] Prep for recording voice --- client/src/components/HumanInput.jsx | 59 +++++++++++++++++++++++++++- 1 file changed, 58 insertions(+), 1 deletion(-) diff --git a/client/src/components/HumanInput.jsx b/client/src/components/HumanInput.jsx index 2a8af1e..7c69a94 100644 --- a/client/src/components/HumanInput.jsx +++ b/client/src/components/HumanInput.jsx @@ -1,6 +1,49 @@ -import React from "react"; +import React, { useState } from "react"; function HumanInput({ onAddNewTopic }) { + const [recording, setRecording] = useState(false); + const [mediaRecorder, setMediaRecorder] = useState(null); + + // Function to start recording + const startRecording = () => { + navigator.mediaDevices + .getUserMedia({ audio: true }) + .then((stream) => { + const newMediaRecorder = new MediaRecorder(stream); + setMediaRecorder(newMediaRecorder); + + newMediaRecorder.start(); + setRecording(true); + + let audioChunks = []; + newMediaRecorder.ondataavailable = (event) => { + audioChunks.push(event.data); + }; + + newMediaRecorder.onstop = () => { + const audioBlob = new Blob(audioChunks, { type: "audio/mp4" }); + const audioUrl = URL.createObjectURL(audioBlob); + + console.log("Stopping recording..."); + + // Optionally handle the audio blob by sending it to a parent component + onAddNewTopic(audioUrl); // Sending the URL for the audio blob to the parent + audioChunks = []; // Clear chunks + }; + }) + .catch((error) => { + console.error("Error accessing the microphone:", error); + }); + }; + + // Function to stop recording + const stopRecording = () => { + if (mediaRecorder) { + mediaRecorder.stop(); + setRecording(false); + } + }; + function handleOnInput(e) { onAddNewTopic(e.target.value); } @@ -12,6 +55,20 @@ function HumanInput({ onAddNewTopic }) { rows="2" placeholder="your input" /> +
+ + +
); }