react-native-vosk icon indicating copy to clipboard operation
react-native-vosk copied to clipboard

Vosk engine stops providing results when used with react-native-background-actions

Open ebano-logsys opened this issue 1 year ago • 2 comments

Issue: Vosk engine stops providing results when used with react-native-background-actions

Description

When using react-native-vosk in conjunction with react-native-background-actions for a voice assistant project, the Vosk engine unexpectedly stops providing new results. This occurs despite the microphone permission remaining active.

Environment

  • React Native version: 0.70.0
  • react-native-vosk version: 0.2.2
  • react-native-background-actions version: 3.0.1
  • react-native-tts version: 4.1.1
  • React version: 18.1.0
  • Device: ANDROID API 35 X86_64

Steps to Reproduce

  1. Set up a React Native project with react-native-vosk and react-native-background-actions
  2. Implement voice recognition using react-native-vosk
  3. Use react-native-background-actions to run the voice recognition in the background
  4. Run the application and observe that after some time, the Vosk engine stops providing new results

Expected Behavior

The Vosk engine should continue to provide speech recognition results while running in the background.

Actual Behavior

After some time, the Vosk engine stops providing new results, even though the microphone permission remains active.

Code Snippet

import React, { useState, useEffect, useRef, useCallback } from 'react';
import { StyleSheet, View, Text, Button } from 'react-native';
import Vosk from 'react-native-vosk';
import BackgroundService from 'react-native-background-actions';
import Tts from 'react-native-tts';

export default function App() {
  const [ready, setReady] = useState(false);
  const [recognizing, setRecognizing] = useState(false);
  const [result, setResult] = useState();

  const vosk = useRef(new Vosk()).current;

  const load = useCallback(async () => {
    vosk
      .loadModel('model-small-es')
      .then(() => setReady(true))
      .catch((e) => console.error(e));
  }, [vosk]);

  const record = async () => {
    vosk
      .start()
      .then(() => {
        console.log('Starting recognition...');
        setRecognizing(true);
      })
      .catch((e) => console.error(e));
  };

  // ... (other methods: recordGrammar, recordTimeout, stop, unload)

  const startBackgroundService = useCallback(async () => {
    const sleep = (time) => new Promise((resolve) => setTimeout(() => resolve(), time));

    const task = async () => {
      await new Promise(async (resolve) => {
        if (!recognizing) {
          console.log(recognizing)
          vosk.loadModel('model-small-es')
            .then(() => {
              setReady(true)
              console.log('hola')
            }).then(() => {
              record();
            })
        }
        await sleep(1000);
      });
    };

    await BackgroundService.start(task, {
      taskName: 'Voice Assistant',
      taskTitle: 'Asistente de Voz activo',
      taskDesc: 'Escuchando comandos de voz...',
      taskIcon: {
        name: 'ic_launcher',
        type: 'mipmap',
      },
      color: '#ff00ff',
      parameters: {
        delay: 1000,
      },
      notifications: {
        color: 'red',
      },
    });
  }, [vosk])

  useEffect(() => {
    const resultEvent = vosk.onResult((res) => {
      console.log('An onResult event has been caught: ' + res);
      setResult(res);
    });

    // ... (other event listeners)

    startBackgroundService();
    return () => {
      resultEvent.remove();
      // ... (remove other event listeners)
    };
  }, [vosk]);

  useEffect(() => {
    if(typeof result == 'string'){
      if(result.toLowerCase().includes('juan confirma mis viajes')){
        console.log('Should work!')
        Tts.speak('Confirmando viajes!')
      }
    }
  }, [result])

  // ... (render method)
}

const styles = StyleSheet.create({
  // ... (styles)
});

Additional Information

  • The issue doesn't seem to be related to microphone permissions, as they remain active.
  • The problem occurs when running the voice recognition in the background using react-native-background-actions.
  • The Vosk engine is initialized and starts correctly, but at some point, it stops providing new results.

Questions

  1. Has anyone encountered a similar issue when using react-native-vosk with background processes?
  2. Are there any known limitations or conflicts between react-native-vosk and react-native-background-actions?
  3. Could this be related to how the Vosk engine handles long-running processes or background execution?
  4. Are there any recommended best practices for using react-native-vosk in a background service?

Troubleshooting Steps Taken

  1. Verified that microphone permissions are still active when the issue occurs.
  2. Checked console logs for any error messages (please provide any relevant logs if available).

Any insights or suggestions for further debugging would be greatly appreciated. Thank you for your time and assistance!

ebano-logsys avatar Sep 25 '24 18:09 ebano-logsys

Hello, my friend, how are you? Were you able to solve the problem?

I’m new to mobile development, but I ran some tests, and on iOS, after just a few minutes of using anything with react-native-background-actions, it stops running. I’ll test it on Android, but on iOS, it seems to be a limitation of the system.

rafaelpoa avatar Jan 05 '25 11:01 rafaelpoa

I think this is an issue with mobile phones permissions in general, but tbh i havent tried it since a while ago. Probably the best direction is to use foreground actions, a project that is called dicio does something like this, but still, i couldnt make it work for my app

ebano-logsys avatar Jan 06 '25 03:01 ebano-logsys