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

174
Views
grabar y detener la funcionalidad de grabación en el mismo botón en React JS

Tengo esta aplicación simple de reaccionar js en la que tengo dos botones, grabar y detener. Todo funciona bien. Ahora, lo que estoy tratando de hacer es que solo debe haber un botón, si no se hace clic, debe mostrar grabar audio, y cuando hago clic en ese botón, debe mostrar detener y debe comenzar a grabar un audio. Intenté hacer esto con el operador coalescente nulo pero no pude encontrar la manera. Alguien por favor ayuda! Aquí está el código:

 import React from 'react'; import './App.css'; import MicRecorder from 'mic-recorder-to-mp3'; const Mp3Recorder = new MicRecorder({ bitRate: 128 }); class App extends React.Component { constructor(props){ super(props); this.state = { isRecording: false, blobURL: '', isBlocked: false, }; } start = () => { if (this.state.isBlocked) { console.log('Permission Denied'); } else { Mp3Recorder .start() .then(() => { this.setState({ isRecording: true }); }).catch((e) => console.error(e)); } }; stop = () => { Mp3Recorder .stop() .getMp3() .then(([buffer, blob]) => { const blobURL = URL.createObjectURL(blob) this.setState({ blobURL, isRecording: false }); }).catch((e) => console.log(e)); }; componentDidMount() { navigator.getUserMedia({ audio: true }, () => { console.log('Permission Granted'); this.setState({ isBlocked: false }); }, () => { console.log('Permission Denied'); this.setState({ isBlocked: true }) }, ); } render(){ return ( <div className="App"> <header className="App-header"> <button onClick={this.start} disabled={this.state.isRecording}>Record</button> <button onClick={this.stop} disabled={!this.state.isRecording}>Stop</button> <audio src={this.state.blobURL} controls="controls" /> </header> </div> ); } } export default App;
about 4 years ago · Santiago Gelvez
2 answers
Answer question

0

Puede usar la evaluación de cortocircuito para generar diferentes botones según la condición: -

 render(){ return ( <div className="App"> <header className="App-header"> {!this.state.isRecording && ( <button onClick={this.start}>Record</button> ); } {this.state.isRecording && ( <button onClick={this.stop}>Stop</button> ); } <audio src={this.state.blobURL} controls="controls" /> </header> </div> ); }
about 4 years ago · Santiago Gelvez Report

0

Deberá cambiar todas las propiedades requeridas en el botón cuando cambie la variable isRecording . De esa manera, no necesita deshabilitar el botón y el método cambiará.

 <button onClick={() => { this.state.isRecording ? this.stop() : this.start()}}>{this.state.isRecording ? "Stop" : "Record"}</button>
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!