Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

333
Views
How to record audio and video using JAVASCRIPT ? (REACT JS)

I have tried but there is an error "TypeError: Cannot read properties of undefined (reading 'stop')" I am calling function on button click here is function as well :)

const getAudio= async ()=>{

         let device = await navigator.mediaDevices.getUserMedia({audio: true});
         let chunks = [];
         let recorder;
         device.then(stream => {
             recorder = new MediaRecorder(stream);
             recorder.ondataavailable = e => {
                 chunks.push(e.data);
                 if (recorder.state === 'inactive') {
                     this.blob = new Blob(chunks, {type: 'audio/webm'});
                     let testAudioRecord  = URL.createObjectURL(this.blob);
                     console.log(testAudioRecord)
                 }
             }
             recorder.start(1000);
         });
         setTimeout(() => {
             recorder.stop();     //   Cannot read ('stop') =error
         }, 2000)
     }

about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

I spot a bit of a mixup between using async with await, and the .then promise handling.

Since you are using an async function, lets refactor to try...catch with await to wait for the promoise to resolve.

  const getAudio = async () => {
    let chunks = [];
    let recorder;

    try {
      //wait for the stream promise to resolve
      let stream = await navigator.mediaDevices.getUserMedia({ audio: true });
      recorder = new MediaRecorder(stream);
      recorder.ondataavailable = (e) => {
        chunks.push(e.data);
        if (recorder.state === "inactive") {
          this.blob = new Blob(chunks, { type: "audio/webm" });
          let testAudioRecord = URL.createObjectURL(this.blob);
          console.log(testAudioRecord);
        }
      };
      recorder.start(1000);

      setTimeout(() => {
        recorder.stop();
      }, 2000);
    } catch (e) {
      console.log("error getting stream", e);
    }
  };

There are better ways to construct the code, but this should get your function working.

Here is a codesandbox with your function and a playback option

Here is another audio codesandbox

about 4 years ago · Santiago Gelvez Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!