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

169
Views
record and stop recording functionality on the same button in the React JS

I have this simple react js application in which I have two buttons, record and stop.Everything is working fine. Now what I am trying to do is there should be only one button, if its not clicked, it should show record audio, and when i click on that button it should show stop and it should start recording an audio. I tried to do this with Nullish coalescing operator but couldn't find a way. Someone please help! Here is the code:

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

You can use short circuit evaluation to render different buttons based on the condition: -

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

You will need to change all the required properties on the button when the isRecording variable changes. That way you don't need to disable the button and the method will change.

<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!