voice icon indicating copy to clipboard operation
voice copied to clipboard

Auto stop or SilenceDetection

Open ivanoldsoul opened this issue 2 years ago • 5 comments
trafficstars

in my app , I want to stop recording if user does not speak for 2 sec. is there any way to achieve this ?

ivanoldsoul avatar Jan 24 '23 13:01 ivanoldsoul

I built this functionality in the app with the following:

let silenceTimer = useRef(null);
Voice.onSpeechResults = onSpeechResults;

const onSpeechResults = (e) => {
    clearTimeout(silenceTimer.current);
    silenceTimer.current = setTimeout(async () => {
      Voice.stop();
    }, 2000);
};

The basic idea is that when voice is detected, onSpeechResults will be called. the silenceTimer is cleared (wont be one on the first call) and then setTimeout is used to stop detection after 2 seconds. If more voice is detected, onSpeechResults will reset the timer each time until 2 seconds of no voice detection happens.

adambeer avatar Feb 08 '23 18:02 adambeer

I built this functionality in the app with the following:

let silenceTimer = useRef(null);
Voice.onSpeechResults = onSpeechResults;

const onSpeechResults = (e) => {
    clearTimeout(silenceTimer.current);
    silenceTimer.current = setTimeout(async () => {
      Voice.stop();
    }, 2000);
};

The basic idea is that when voice is detected, onSpeechResults will be called. the silenceTimer is cleared (wont be one on the first call) and then setTimeout is used to stop detection after 2 seconds. If more voice is detected, onSpeechResults will reset the timer each time until 2 seconds of no voice detection happens.

it can work for ios but on android it auto stops recording after detecting silence

codding123vbf avatar Feb 21 '24 13:02 codding123vbf

I built this functionality in the app with the following:

let silenceTimer = useRef(null);
Voice.onSpeechResults = onSpeechResults;

const onSpeechResults = (e) => {
    clearTimeout(silenceTimer.current);
    silenceTimer.current = setTimeout(async () => {
      Voice.stop();
    }, 2000);
};

The basic idea is that when voice is detected, onSpeechResults will be called. the silenceTimer is cleared (wont be one on the first call) and then setTimeout is used to stop detection after 2 seconds. If more voice is detected, onSpeechResults will reset the timer each time until 2 seconds of no voice detection happens.

best anw thanks brother perfectly working for IOS . when user stop speaking voice rec auto stop after 5 sec

ahaseeb001 avatar Apr 29 '24 11:04 ahaseeb001

In my case it is best way to handle it inside TS-code by results from micro.

  useEffect(() => {
    if (isEmpty(results)) {
      const newTimer = setTimeout(() => {
        _stopRecognizing();
      }, 10000); // 10 seconds

      setTimer(newTimer);

      return () => {
        if (timer) {
          clearTimeout(timer);
        }
      };
    }
  }, [results]);

YaremaV avatar Jul 23 '24 08:07 YaremaV