voice icon indicating copy to clipboard operation
voice copied to clipboard

Voice recognition stops automatically after few seconds

Open aparnagude396 opened this issue 2 years ago • 27 comments

Hi, I'm using react-native-community/voice library in my project for speech to text feature. But, in android the voice recognition is stops automatically after 3 to 4 seconds. It is not activating again when we speaks. For this process again I have to close the popup and again I have to click on mike button then it is recognizing but same issue after 3 to 4 secs it stops listening. If anyone got the solution for this, please help me.

aparnagude396 avatar Aug 30 '22 10:08 aparnagude396

Same problem

paskalov-aris avatar Sep 01 '22 13:09 paskalov-aris

I think the issue here is that it uses on Android this API: Android SpeechRecognizer

On that documentation it states

The implementation of this API is likely to stream audio to remote servers to perform speech recognition. As such this API is not intended to be used for continuous recognition, which would consume a significant amount of battery and bandwidth.

fobin avatar Sep 05 '22 07:09 fobin

Hi @fobin, Thanks for the reply. I resolved this issue like I'm closing the popup whenever user stops speaking. So that I'm taking the whatever results are coming in that response. If user wants to speak again he has click on mike button. By using this process It's working perfectly.

aparnagude396 avatar Sep 05 '22 07:09 aparnagude396

Great day to all, I have tried to implement this library and I have not yet been able to get it to translate a phrase like "Hello World", I downloaded the example provided in the repository I have used it with React-Native CLI and also with EXPO for Android, the application runs but it does not recognize any phrase. @aparnagude396 Could you share some snippets of your code and some configuration considerations, I would be very grateful.

raulgonzalezdev avatar Sep 05 '22 10:09 raulgonzalezdev

Hi @gqcryptoraul, I'm calling this method while user click on mike button. startSpeechRecognizing = async () => { await this.checkPermission(); var language = await AsyncStorage.getItem('SelectedLanguge'); var lan='mr-IN' if (language == 'English') { lan='en-US'; } if (language == 'Marathi') { lan='mr-IN' } if (language == 'Hindi') { lan='hi-IN' } if (language == 'Gujarati') { lan='gu-IN' } try { this.setState({ speechDialog: true,otherQuery:'' }); await Voice.start(lan, { "RECOGNIZER_ENGINE": "GOOGLE", "EXTRA_PARTIAL_RESULTS": true }); } catch (e) { console.error(e); } };

aparnagude396 avatar Sep 05 '22 10:09 aparnagude396

Thanks, @aparnagude396 I attach my code to see if you can take a look at it. Still not working as expected for me.

import React, { useEffect, useState } from 'react'; import { StyleSheet, Text, View, Image, TouchableHighlight, Button, } from 'react-native';

import Voice, { SpeechRecognizedEvent, SpeechResultsEvent, SpeechErrorEvent, } from '@react-native-voice/voice';

function VoiceTest() { const [recognized, setRecognized] = useState(''); const [volume, setVolume] = useState(''); const [error, setError] = useState(''); const [end, setEnd] = useState(''); const [started, setStarted] = useState(''); const [results, setResults] = useState([]); const [partialResults, setPartialResults] = useState([]);

useEffect(() => { Voice.onSpeechStart = onSpeechStart; Voice.onSpeechRecognized = onSpeechRecognized; Voice.onSpeechEnd = onSpeechEnd; Voice.onSpeechError = onSpeechError; Voice.onSpeechResults = onSpeechResults; Voice.onSpeechPartialResults = onSpeechPartialResults; Voice.onSpeechVolumeChanged = onSpeechVolumeChanged; console.log("is avilable-->"+JSON.stringify(Voice.isAvailable()))

return () => {
  Voice.destroy().then(Voice.removeAllListeners);
};

}, []);

const onSpeechStart = (e: any) => { console.log('onSpeechStart: ', e); setStarted('√'); };

const onSpeechRecognized = (e: SpeechRecognizedEvent) => { console.log('onSpeechRecognized: ', e); setRecognized('√'); };

const onSpeechEnd = (e: any) => { console.log('onSpeechEnd: ', e); setEnd('√'); };

const onSpeechError = (e: SpeechErrorEvent) => { console.log('onSpeechError: ', e); setError(JSON.stringify(e.error)); };

const onSpeechResults = (e: SpeechResultsEvent) => { console.log('onSpeechResults: ', e); setResults(e.value); };

const onSpeechPartialResults = (e: SpeechResultsEvent) => { console.log('onSpeechPartialResults: ', e); setPartialResults(e.value); };

const onSpeechVolumeChanged = (e: any) => { console.log('onSpeechVolumeChanged: ', e); setVolume(e.value); };

const _startRecognizing = async () => { console.log('Intento de Reconocer called start'); _clearState(); try { await Voice.start('en-US',{ "RECOGNIZER_ENGINE": "GOOGLE", "EXTRA_PARTIAL_RESULTS": true }); console.log('called start'); } catch (e) { console.error(e); } };

const _stopRecognizing = async () => { console.log('Intento de Stop'); try { await Voice.stop(); console.log('called stop'); } catch (e) { console.error(e); } };

const _cancelRecognizing = async () => { try { await Voice.cancel(); } catch (e) { console.error(e); } };

const _destroyRecognizer = async () => { try { await Voice.destroy(); } catch (e) { console.error(e); } _clearState(); };

const _clearState = () => { setRecognized(''); setVolume(''); setError(''); setEnd(''); setStarted(''); setResults([]); setPartialResults([]); };

return (

<View style={styles.container}>
  <Text style={styles.welcome}>Welcome to React Native Voice RRR!</Text>
  <Text style={styles.instructions}>
    Press the button and start speaking.
  </Text>
  <Text style={styles.stat}>{`Started: ${started}`}</Text>
  <Text style={styles.stat}>{`Recognized: ${recognized}`}</Text>
  <Text style={styles.stat}>{`Volume: ${volume}`}</Text>
  <Text style={styles.stat}>{`Error: ${error}`}</Text>
  <Text style={styles.stat}>Results</Text>
  <>
  {console.log('results', results)}
  </>
  {results.map((result, index) => {
    return (
      <Text key={`result-${index}`} style={styles.stat}>
        {result}
      </Text>
    );
  })}
  <Text style={styles.stat}>Partial Results</Text>
  {partialResults.map((result, index) => {
    return (
      <Text key={`partial-result-${index}`} style={styles.stat}>
        {result}
      </Text>
    );
  })}
  
  <Text style={styles.stat}>{`End: ${end}`}</Text>
  <TouchableHighlight onPress={_startRecognizing}>
    <Image style={styles.button} source={require('./button.png')} />
  </TouchableHighlight>
  <TouchableHighlight onPress={_stopRecognizing}>
    <Text style={styles.action}>Stop Recognizing</Text>
  </TouchableHighlight>
  <TouchableHighlight onPress={_cancelRecognizing}>
    <Text style={styles.action}>Cancel</Text>
  </TouchableHighlight>
  <TouchableHighlight onPress={_destroyRecognizer}>
    <Text style={styles.action}>Destroy</Text>
  </TouchableHighlight>
  <Button
    title={"Start Recognizing"}
    onPress={_startRecognizing}
  />
</View>

); }

const styles = StyleSheet.create({ button: { width: 50, height: 50, }, container: { flex: 1, justifyContent: 'center', alignItems: 'center', backgroundColor: '#F5FCFF', }, welcome: { fontSize: 20, textAlign: 'center', margin: 10, }, action: { textAlign: 'center', color: '#0000FF', marginVertical: 5, fontWeight: 'bold', }, instructions: { textAlign: 'center', color: '#333333', marginBottom: 5, }, stat: { textAlign: 'center', color: '#B0171F', marginBottom: 1, }, });

export default VoiceTest;

**I leave you the code I'm using, it still works for me, this is the example that is in the community repository.

I attached this code to the configuration of App.Json**

"plugins": [ [ "@react-native-voice/voice", { "microphonePermission": "Allow $(PRODUCT_NAME) to access your microphone", "speechRecognitionPermission": "Allow $(PRODUCT_NAME) to securely recognize user speech" } ] ],

and when you open the App for the first time it gives permissions to the Microphone, but the App does not respond

I make a console.log console.log("is avilable-->"+JSON.stringify(Voice.isAvailable())) and it returns this.

is avilable-->{"_1":0,"_2":0,"_3":null,"_4":null}

and the result object always returns []

Could you share a working example in a repository where I can do a fork and see where I am going wrong?

raulgonzalezdev avatar Sep 05 '22 13:09 raulgonzalezdev

I have done something similar as already proposed elsewhere.

I used onSpeechPartialResults due to onSpeechResults not working well on Android.

I have also added onSpeechEnd and onSpeechError to re-activate the voice recognition when it gets stopped on Android.

I've also made sure the voice process is killed when not needed anymore.

iOS performs way better, way way better.

matteosprintdigital avatar Oct 12 '22 05:10 matteosprintdigital

I have done something similar as already proposed elsewhere.

I used onSpeechPartialResults due to onSpeechResults not working well on Android.

I have also added onSpeechEnd and onSpeechError to re-activate the voice recognition when it gets stopped on Android.

I've also made sure the voice process is killed when not needed anymore.

iOS performs way better, way way better.

Can you please share code example

HasibRawal avatar Nov 05 '22 20:11 HasibRawal

Hi, I'm having the same problem with the sample app on Android phones. The detection stops after a few seconds. I'm running this on Samsung S20 and S21 Ultra, both on Android 12.

mwasiel78 avatar Nov 08 '22 19:11 mwasiel78

Hi, I'm having the same problem with the sample app on Android phones. The detection stops after a few seconds. I'm running this on Samsung S20 and S21 Ultra, both on Android 12.

I am able to continue dictation till 10 seconds even after user stops talking

HasibRawal avatar Nov 09 '22 10:11 HasibRawal

_ @aparnagude396 @fobin @HasibRawal @paskalov-aris

#22

finally i am done with that issue and solve this issue with little bit logic. please check below code which i added.

1.You have to install react-native-voice first all. 2.you have to make file like VoiceSopcher or any think. 3. add this code on this file which i add below.

import {NativeModules, NativeEventEmitter, Platform} from 'react-native';

const {Voice} = NativeModules;

// NativeEventEmitter is only availabe on React Native platforms, so this conditional is used to avoid import conflicts in the browser/server const voiceEmitter = Platform.OS !== 'web' ? new NativeEventEmitter(Voice) : null;

class RCTVoice { constructor() { this._loaded = false; this._listeners = null; this._events = { onSpeechStart: this._onSpeechStart.bind(this), onSpeechRecognized: this._onSpeechRecognized.bind(this), onSpeechEnd: this._onSpeechEnd.bind(this), onSpeechError: this._onSpeechError.bind(this), onSpeechResults: this._onSpeechResults.bind(this), onSpeechPartialResults: this._onSpeechPartialResults.bind(this), onSpeechVolumeChanged: this._onSpeechVolumeChanged.bind(this), }; } removeAllListeners() { Voice.onSpeechStart = null; Voice.onSpeechRecognized = null; Voice.onSpeechEnd = null; Voice.onSpeechError = null; Voice.onSpeechResults = null; Voice.onSpeechPartialResults = null; Voice.onSpeechVolumeChanged = null; } destroy() { if (!this._loaded && !this._listeners) { return Promise.resolve(); } return new Promise((resolve, reject) => { Voice.destroySpeech(error => { if (error) { reject(new Error(error)); } else { if (this._listeners) { this._listeners.map(listener => listener.remove()); this._listeners = null; } resolve(); } }); }); }

onStartAgain(locale, options = {}) { if (!this._loaded && !this._listeners && voiceEmitter !== null) { this._listeners = Object.keys(this._events).map(key => { return voiceEmitter.addListener(key, this._events[key]); }); }

return new Promise((resolve, reject) => {
  const callback = error => {
    if (error) {
      reject(new Error(error));
    } else {
      resolve();
    }
  };
  if (Platform.OS === 'android') {
    Voice.startSpeech(
      locale,
      Object.assign(
        {
          EXTRA_LANGUAGE_MODEL: 'LANGUAGE_MODEL_FREE_FORM',
          EXTRA_MAX_RESULTS: 5,
          EXTRA_PARTIAL_RESULTS: true,
          REQUEST_PERMISSIONS_AUTO: true,
        },
        options,
      ),
      callback,
    );
  } else {
    Voice.startSpeech(locale, callback);
  }
});

}

start(locale, options = {}) { console.log('isStart ==> '); if (!this._loaded && !this._listeners && voiceEmitter !== null) { console.log('_listeners ==> '); this._listeners = Object.keys(this._events).map(key => { console.log('keys ==> ', key); return voiceEmitter.addListener(key, this._events[key]); }); }

return new Promise((resolve, reject) => {
  const callback = error => {
    console.log('CallBack');
    if (error) {
      console.log('CallBack error');
      reject(new Error(error));
    } else {
      console.log('CallBack resolve');
      resolve();
    }
  };
  if (Platform.OS === 'android') {
    Voice.startSpeech(
      locale,
      Object.assign(
        {
          EXTRA_LANGUAGE_MODEL: 'LANGUAGE_MODEL_FREE_FORM',
          EXTRA_MAX_RESULTS: 5,
          EXTRA_PARTIAL_RESULTS: true,
          REQUEST_PERMISSIONS_AUTO: true,
        },
        options,
      ),
      callback,
    );
  } else {
    Voice.startSpeech(locale, callback);
  }
});

} stop() { if (!this._loaded && !this._listeners) { console.log('stop pela=>'); return Promise.resolve(); } return new Promise((resolve, reject) => { Voice.stopSpeech(error => { console.log('stop =>'); if (error) { reject(new Error(error)); } else { resolve(); } }); }); } cancel() { if (!this._loaded && !this._listeners) { console.log('cancel pela=>');

  return Promise.resolve();
}
return new Promise((resolve, reject) => {
  Voice.cancelSpeech(error => {
    console.log('cancelSpeech pela=>');

    if (error) {
      reject(new Error(error));
    } else {
      resolve();
    }
  });
});

} isAvailable() { return new Promise((resolve, reject) => { Voice.isSpeechAvailable((isAvailable, error) => { if (error) { reject(new Error(error)); } else { resolve(isAvailable); } }); }); }

/**

  • (Android) Get a list of the speech recognition engines available on the device
  • */ getSpeechRecognitionServices() { if (Platform.OS !== 'android') { throw new Exception( 'Speech recognition services can be queried for only on Android', ); }
return Voice.getSpeechRecognitionServices();

}

isRecognizing() { return new Promise(resolve => { Voice.isRecognizing(isRecognizing => resolve(isRecognizing)); }); } _onSpeechStart(e) { if (this.onSpeechStart) { this.onSpeechStart(e); } } _onSpeechRecognized(e) { if (this.onSpeechRecognized) { this.onSpeechRecognized(e); } } _onSpeechEnd(e) { if (this.onSpeechEnd) { this.onSpeechEnd(e); } } _onSpeechError(e) { if (this.onSpeechError) { this.onSpeechError(e); } } _onSpeechResults(e) { if (this.onSpeechResults) { this.onSpeechResults(e); } } _onSpeechPartialResults(e) { if (this.onSpeechPartialResults) { this.onSpeechPartialResults(e); } } _onSpeechVolumeChanged(e) { if (this.onSpeechVolumeChanged) { this.onSpeechVolumeChanged(e); } } }

export default new RCTVoice();

After That you have to add some logic in your voice file you have call one function it's onStartAgain like see in below code

// Voice useEffect(() => { //All Voice Result Voice.onSpeechStart = e => {}; Voice.onSpeechPartialResults = e => {}; Voice.onSpeechRecognized = e => { console.log('onSpeechRecognized => ', e); }; Voice.onSpeechResults = e => { SpeechResultAppend(e); }; Voice.onSpeechError = e => { console.log('onSpeechError => ', e); setSpeechError(true); }; return () => { Voice.destroy().then(Voice.removeAllListeners); }; }, []);

//SpeechResultAppend const SpeechResultAppend = VoiceText => { console.log('VoiceText => ', VoiceText); let {value} = VoiceText; let TextPoint = ''; value?.map((result, index) => { TextPoint = result; }); // append data with write message setChatMessage(pres_state => pres_state + ' ' + TextPoint); //StartAgain StartAgainPort(); // when user stop speak };

You have to call this when user stop speck // /StartAgainPort const StartAgainPort = async () => { setSpeechError(false); setPressInOut(true); await Voice.onStartAgain('en-US', { RECOGNIZER_ENGINE: 'services', EXTRA_PARTIAL_RESULTS: true, }); };

// // _OnPressIn const _OnPressMicroPhone = async () => { setSpeechError(false); setPressInOut(true); await Voice.start('en-US', { RECOGNIZER_ENGINE: 'services', EXTRA_PARTIAL_RESULTS: true, }); };

// // /_OnPressOut const _OnPressOutMicroPhone = async () => { console.log('_OnPressOutMicroPhone => '); setSpeechError(false); setPressInOut(false); await Voice.destroy(); };

jaydevhapani avatar Feb 23 '23 04:02 jaydevhapani

Hi Same issue for me Any update? Thanks

ashirkhan94 avatar May 26 '23 06:05 ashirkhan94

Anyone looking for an answer, This is what I found. You can keep the recognizer turned on for longer period of time by setting EXTRA_SPEECH_INPUT_COMPLETE_SILENCE_LENGTH_MILLIS to n number of miliseconds.

Voice.start("en-US", {
       // Complete silence for 10 seconds
	EXTRA_SPEECH_INPUT_COMPLETE_SILENCE_LENGTH_MILLIS: 10000,
});

Aparently, it a feature to save battery to stop recognizer after few seconds of silence. Above setting will increase the silence time to 10 seconds. Reference

11kodykay11 avatar Sep 27 '23 03:09 11kodykay11

any updates Please ? Because of this i can't live my app in android

ahsanbhatti98 avatar Jan 11 '24 11:01 ahsanbhatti98

I'm also getting the same issue I want continous voice recognization because when I take a samll pause then it stops working.

AyazQadri avatar Feb 03 '24 12:02 AyazQadri

did someone find any fix???

codding123vbf avatar Feb 15 '24 11:02 codding123vbf

I'm also getting the same issue I want continous voice recognization because when I take a samll pause then it stops working.

have you found any alternative to this library bro ?

codding123vbf avatar Feb 15 '24 11:02 codding123vbf

No i have used the same library by doing some modifications

On Thu, 15 Feb 2024 at 4:43 PM, codding123vbf @.***> wrote:

I'm also getting the same issue I want continous voice recognization because when I take a samll pause then it stops working.

have you found any alternative to this library bro ?

— Reply to this email directly, view it on GitHub https://github.com/react-native-voice/voice/issues/402#issuecomment-1945877462, or unsubscribe https://github.com/notifications/unsubscribe-auth/AHGGT5RJW45SRY2HNZ6M543YTXUU7AVCNFSM6AAAAAAQAIMNEGVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTSNBVHA3TONBWGI . You are receiving this because you were mentioned.Message ID: @.***>

HasibRawal avatar Feb 15 '24 11:02 HasibRawal

No i have used the same library by doing some modifications On Thu, 15 Feb 2024 at 4:43 PM, codding123vbf @.> wrote: I'm also getting the same issue I want continous voice recognization because when I take a samll pause then it stops working. have you found any alternative to this library bro ? — Reply to this email directly, view it on GitHub <#402 (comment)>, or unsubscribe https://github.com/notifications/unsubscribe-auth/AHGGT5RJW45SRY2HNZ6M543YTXUU7AVCNFSM6AAAAAAQAIMNEGVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTSNBVHA3TONBWGI . You are receiving this because you were mentioned.Message ID: @.>

does it work like built in voice to text service ? can you share ur code pls ?

codding123vbf avatar Feb 15 '24 11:02 codding123vbf

Hi @HasibRawal

Can u please share code snippets ?

AyazQadri avatar Feb 15 '24 11:02 AyazQadri

I'm also getting the same issue I want continous voice recognization because when I take a samll pause then it stops working.

have you found any alternative to this library bro ?

Unfortunately, No.

AyazQadri avatar Feb 15 '24 11:02 AyazQadri

I'm also getting the same issue I want continous voice recognization because when I take a samll pause then it stops working.

have you found any alternative to this library bro ?

Unfortunately, No.

same bro im also stuck in it and there is no alternative packages :(

codding123vbf avatar Feb 15 '24 11:02 codding123vbf

Hi @HasibRawal

Can u please share code snippets ?

useEffect(()=>{ Voice. onSpeechStart = onSpeechStart; Voice. onSpeechRecognized = onSpeechRecognized; Voice. onSpeechEnd = onSpeechEnd; Voice.onSpeechError = onSpeechError; Voice.onSpeechResults = onSpeechResults; Voice. onSpeechPartialResults = onSpeechPartialResults; Voice. onSpeechVolumeChanged = onSpeechVo lumeChanged; return () => { Voice. destroy(). then (Voice, removeAllListeners); }; },[]); const onSpeechEnd = (e: any) => { setTimeout(() => { _startRecognizing(); },2000); const onSpeechError = (e: SpeechErrorEvent) => { setError (JSON. stringify(e.error)); }; const onSpeechResults = (e: SpeechResultsEvent) => if (!e.value) ( return; setText (e. value [0]) ; const onSpeechPartialResults = (et SpeechResultsEvent) = ( if (le,value) { return; } setPartialResults (I.•partialResults, e.value [0]]); setText (e. value [0]) ; }

const _startRecognizing = async () = { _clearStatel; try { await Voice.start('en-Us', f EXTRA_PARTIAL_RESULTS: false, EXTRA_SPEECH_INPUT_POSSIBLY_COMPLETE_SILENCE_LENGTH_MILLIS: 50000000, EXTRA_SPEECH_INPUT_COMPLETE_SILENCE_LENGTH_MILLIS: 50000000, } catch (e) { console.error(e); } }; const _cancelRecognizing = async () => { try { await Voice. cancel(); } catch (e) { console. error(e); const _destroyRecognizer = async () => { try } await Voice. destroy(); catch (e) { console.error(e); _clearState(); }

HasibRawal avatar Feb 15 '24 12:02 HasibRawal

Thanks @HasibRawal for the code.

I'm using the react-native-community/voice package for speech-to-text functionality. Currently, when I start speaking, the transcription stops if I pause, and sometimes it stops even when I'm still speaking. I want the transcription to continue uninterrupted, similar to how it works in WhatsApp.

Additionally, when I set the following values in an object, the app crashes on Android (I haven't tested on iOS) when the specified time limit is reached, which shouldn't happen:

  • EXTRA_PARTIAL_RESULTS: false
  • EXTRA_SPEECH_INPUT_POSSIBLY_COMPLETE_SILENCE_LENGTH_MILLIS: 50000000
  • EXTRA_SPEECH_INPUT_COMPLETE_SILENCE_LENGTH_MILLIS: 50000000

AyazQadri avatar Feb 15 '24 13:02 AyazQadri

Thanks @HasibRawal for the code.

I'm using the react-native-community/voice package for speech-to-text functionality. Currently, when I start speaking, the transcription stops if I pause, and sometimes it stops even when I'm still speaking. I want the transcription to continue uninterrupted, similar to how it works in WhatsApp.

Additionally, when I set the following values in an object, the app crashes on Android (I haven't tested on iOS) when the specified time limit is reached, which shouldn't happen:

  • EXTRA_PARTIAL_RESULTS: false
  • EXTRA_SPEECH_INPUT_POSSIBLY_COMPLETE_SILENCE_LENGTH_MILLIS: 50000000
  • EXTRA_SPEECH_INPUT_COMPLETE_SILENCE_LENGTH_MILLIS: 50000000

same happening to me bro...and i also want to implement speech recognition like whatsapp as you mentioned in your comment...but unfortuantely we are stuck with this non-maintianed package and there are no alternatives to it sadly :(

codding123vbf avatar Feb 15 '24 13:02 codding123vbf

Hi Same issue Any update? Thanks

greenraze13 avatar Jul 09 '24 11:07 greenraze13

How to fix android . Its not working a have problem Record Started onSpeechStart {"error":false}

fturkyilmaz avatar Jul 18 '24 18:07 fturkyilmaz