Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug: listener is not executed on android #91

Closed
klolo opened this issue May 26, 2024 · 3 comments
Closed

bug: listener is not executed on android #91

klolo opened this issue May 26, 2024 · 3 comments

Comments

@klolo
Copy link

klolo commented May 26, 2024

Hi!
I have a problem on Android device . SpeechRecognition listener is not executed and I cannot fetch recorded text... Could you take a look at my component?

import { SpeechRecognition } from '@capacitor-community/speech-recognition';
import { Box } from '@chakra-ui/react';
import { FC, useEffect, useState } from 'react';
import '../SentenceSpeakExercise.css';

export interface SentenceSpeakExerciseNativeProps {
  recordingActive: boolean | null;
  onRecordingFinished: () => void;
  onRecordAvailable: (sentence: string) => void;
}

export const SentenceSpeakExerciseNative: FC<SentenceSpeakExerciseNativeProps> = ({ recordingActive, onRecordAvailable }) => {
  const [nativeSpeechRecognitionAvailable, setNativeSpeechRecognitionAvailable] = useState<boolean | null>(false);

  useEffect(() => {
    SpeechRecognition.available()
      .then((result) => setNativeSpeechRecognitionAvailable(result.available))
      .catch((err) => {
        console.log('SpeechRecognition.available() failed:', err);
      });
  }, []);

  const startRecording = async () => {
    try {
      console.log('start recording...'); // I can see this in logs
      await SpeechRecognition.requestPermissions();

      SpeechRecognition.addListener('partialResults', (data) => {
        // this is not executed....
        console.log('partialResults was fired', JSON.stringify(data));
        onRecordAvailable('FIXME');
      });

      await SpeechRecognition.start({
        language: 'en-US',
        maxResults: 1,
        prompt: '',
        partialResults: true,
        popup: false,
      });
    } catch (error) {
      console.error('Error starting recording:', error);
    }
  };

  const stopRecording = async () => {
    try {
      console.log('stop recording...');
      SpeechRecognition.removeAllListeners();
      await SpeechRecognition.stop();
    } catch (error) {
      console.error('Error stopping recording:', error);
    }
  };

  useEffect(() => {
    if (recordingActive) {
      startRecording();
    } else {
      stopRecording();
    }

    // Clean up listeners when component unmounts or recordingActive changes
    return () => {
      SpeechRecognition.removeAllListeners();
    };
  }, [recordingActive]);

  return (
    <Box>
      {nativeSpeechRecognitionAvailable && <Box>native speech ready</Box>}
      {nativeSpeechRecognitionAvailable === false && <Box>native not available</Box>}
    </Box>
  );
};

Could you help me and tell is it bug in plugin or my mistake?

Used versions:

   "@capacitor-community/speech-recognition": "^5.1.0",
    "@capacitor/android": "^5.0.0",
    "@capacitor/cli": "^5.0.0",
    "@capacitor/core": "^5.0.0",
    "@capacitor/status-bar": "^5.0.6"`
@klolo
Copy link
Author

klolo commented May 29, 2024

I found that with popup I receive speech but this listener and partial results doesnt work...

@jcesarmobile
Copy link
Contributor

Yeah, it's documented that if using the popup the partialResults won't fire, it's a limitation on the popup.

https://github.com/capacitor-community/speech-recognition?tab=readme-ov-file#addlistenerpartialresults-

@jcesarmobile jcesarmobile closed this as not planned Won't fix, can't repro, duplicate, stale Jun 7, 2024
@klolo
Copy link
Author

klolo commented Jun 7, 2024

Yes, I know it...but my problem is not for popup. Check first comment

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 3, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants