Skip to content

Commit

Permalink
Prep for recording voice
Browse files Browse the repository at this point in the history
  • Loading branch information
albin-karlsson committed Apr 26, 2024
1 parent 60a598b commit 1496c5b
Showing 1 changed file with 58 additions and 1 deletion.
59 changes: 58 additions & 1 deletion client/src/components/HumanInput.jsx
Original file line number Diff line number Diff line change
@@ -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);
}
Expand All @@ -12,6 +55,20 @@ function HumanInput({ onAddNewTopic }) {
rows="2"
placeholder="your input"
/>
<div>
<button
onClick={startRecording}
disabled={recording}
>
Start Recording
</button>
<button
onClick={stopRecording}
disabled={!recording}
>
Stop Recording
</button>
</div>
</div>
);
}
Expand Down

0 comments on commit 1496c5b

Please sign in to comment.