JamesBrill / react-speech-recognition

💬Speech recognition for your React app
https://webspeechrecognition.com/
MIT License
695 stars 122 forks source link

'abortListening' or 'stopListening' not working #9

Closed QishengLi closed 5 years ago

QishengLi commented 5 years ago

Hi I am new to React and trying to use react-speech-recognition. I am trying to start & stop listening to the microphone by click buttons. However, it seems that it's still listening after I click 'stop' button and the transcription is still updating.

Attaching the code here. Any advice will be helpful!

import React, { PropTypes, Component } from 'react'
import SpeechRecognition from 'react-speech-recognition'

const propTypes = {
  // Props injected by SpeechRecognition
  transcript: PropTypes.string,
  finalTranscript: PropTypes.string,
  startListening: PropTypes.func,
  abortListening: PropTypes.func,
  resetTranscript: PropTypes.func,
  browserSupportsSpeechRecognition: PropTypes.bool
}

const options = {
  autoStart: false
}

class Dictaphone extends Component {

  render() {
    const { 
      transcript, 
      finalTranscript,
      startListening, 
      abortListening, 
      resetTranscript, 
      browserSupportsSpeechRecognition 
    } = this.props

    if (!browserSupportsSpeechRecognition) {
      return null
    }

    return (
      <div>
        <button onClick={startListening}>Start</button>
        <button onClick={abortListening}>Stop</button>
        <button onClick={resetTranscript}>Reset</button>
        <p>{finalTranscript}</p>
      </div>
    )
  }
}

Dictaphone.propTypes = propTypes

export default SpeechRecognition(options)(Dictaphone)

(Sorry for the bad formatting!)

JamesBrill commented 5 years ago

Hi Qisheng, I have been unable to reproduce your issue - using your exact code, stopListening and abortListening work correctly for me.

Some questions:

Some tips:

QishengLi commented 5 years ago

Thank you a lot, James! I figured it out that the reason was Dictaphonebeing rendered multiple times.