Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

336
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda