Audio Recording Stop

I’m working on a React application where I need to automatically stop the audio recording when silence is detected. I’m using the ReactMediaRecorder component for recording audio. Currently, I have a stop button that stops the recording when clicked, but I want the recording to stop automatically when there’s a period of silence.

Here is my current implementation:

import React, {useEffect, useRef} from "react";
import { Button } from "antd";
import { AudioOutlined, StopOutlined } from "@ant-design/icons";
import { ReactMediaRecorder } from "react-media-recorder";
import '../../style/InterviewWithAi.css'
import axios from "axios";
import { useSelector } from "react-redux";
import {  toast } from 'react-toastify';
import { useDispatch } from 'react-redux'
import {setResult} from '../../redux/interviewReducer'
import { useNavigate } from "react-router-dom";

function InterviewWithAi() {
  const videoRef = useRef(null);
  useEffect(() => {
    const startVideo = async () => {
      try {
        const stream = await navigator.mediaDevices.getUserMedia({ video: true });
        if (videoRef.current) {
          videoRef.current.srcObject = stream;
        }
      } catch (error) {
        console.error('Error accessing user camera: ', error);
      }
    };

    startVideo();

    const currentVideoRef = videoRef.current;

    return () => {
      if (currentVideoRef && currentVideoRef.srcObject) {
        const tracks = currentVideoRef.srcObject.getTracks();
        tracks.forEach(track => track.stop());
      }
    };
  }, []);
  const dispatch = useDispatch()
  const navigate = useNavigate();
  
  const postition = useSelector((state) => state.interviewReducer.isPosition)
  const level = useSelector((state) => state.interviewReducer.isLevel)
  const candidate = useSelector((state) => state.interviewReducer.isCandidate)
  console.log(postition,level,candidate);
  const handleStopClick = async (blobUrl,blob) => {
    try {
      const data = new FormData()
      data.append('file',blob,'myvoice.wav')
      data.append('position',postition)
      data.append('level',level)
      data.append('candidate',candidate)
      const response = await axios.post('https://www.virtualmunim.in/api/talk',data,{
        headers: {
          'Content-Type': "multipart/form-data"
        },
        responseType: "arraybuffer"
      })
      if (response.status === 200) {
        console.log(response.data);
        const data = response.data
        const blobMpeg = new Blob([data],{type: "audio/mpeg"})
        const audio = new Audio()
        audio.src = window.URL.createObjectURL(blobMpeg)
        audio.play()
      }
    } catch (error) {
      console.log('Error While Fetching: ',error);
    }
  };

  async function handleClearHistoryClick() {
    try {
      const response = await axios.post('https://www.virtualmunim.in/api/cleardata',{
        candidate: candidate
      })
      if (response.status === 200) {
       toast.success("History Deleted") 
      }
    } catch (error) {
      console.log('Error in deleting', error);
    }
  }

  async function handleResult() {
    try {
      const response = await axios.post('https://www.virtualmunim.in/api/interviewresult',{
        candidate: candidate
      })
      if (response.status === 200) {
        console.log(response.data);
        dispatch(setResult(response.data))
        navigate('/interviewresult')
      }
    } catch (error) {
      console.log('Error While Fetching: ',error);
    }
  }
  return (
    <ReactMediaRecorder
      audio
      onStop={handleStopClick}
      render={({ status, startRecording, stopRecording }) => (
        <div className="InterviewWithAi">
          <Button
            className={`record-button ${status === "recording" ? "active" : ""}`}
            shape="circle"
            icon={<AudioOutlined />}
            size="large"
            onClick={startRecording}
          />
          <Button
            className={`stop-button ${status === "recording" ? "active" : ""}`}
            shape="circle"
            icon={<StopOutlined />}
            size="large"
            onClick={stopRecording}
          />
          <Button
            className="clear-button"
            shape="rectangle"
            size="large"
            onClick={handleClearHistoryClick}
          >
            Clear History
          </Button>
          <video ref={videoRef} autoPlay muted className="InterviewWithAiVideo"></video>
          <Button
            className="InterviewWithAiResult"
            shape="rectangle"
            size="large"
            onClick={handleResult}
          >
            Result
          </Button>
        </div>
      )}
    />
  );
}

export default InterviewWithAi;

Could anyone provide guidance or examples on how to detect silence in the audio stream and automatically stop the recording when silence is detected?

Additional context:

I’m using the react-media-recorder package for recording audio.
The stop button currently stops the recording manually, but I need to implement automatic stopping on silence detection.
Thank you in advance for your help!

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật