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: SpeechRecognition.stop() won't stop recording on android. #101

Open
sfcheng opened this issue Jun 22, 2024 · 3 comments
Open

bug: SpeechRecognition.stop() won't stop recording on android. #101

sfcheng opened this issue Jun 22, 2024 · 3 comments

Comments

@sfcheng
Copy link

sfcheng commented Jun 22, 2024

Plugin version:

@capacitor-community/[email protected]

Platform(s):

Android 12

Current behavior:

By default, android will stop listening when there is no more speech input detected. However, when the app is run in a place with a lot of background noise, the recording process will never stop even if I call SpeechRecognition.stop(). I am not using the android popup.

Expected behavior:

Calling SpeechRecognition.stop() should stop the recording.

Steps to reproduce:

Start the record in a noisy place or while playing some background music.

@GerardoPrototype
Copy link

I found the solution
replace the function that stops listening on android

private void stopListening() {
        bridge
            .getWebView()
            .post(
                () -> {
                    try {
                        SpeechRecognition.this.lock.lock();
                        if (SpeechRecognition.this.listening) {
                            if (speechRecognizer != null) {
                                speechRecognizer.stopListening();
                                speechRecognizer.cancel();  // Cancel ongoing recognition
                                speechRecognizer.destroy(); // Destroy the recognizer to release resources
                                speechRecognizer = null;
                            }
                            SpeechRecognition.this.listening(false);
                            
                            // Notify listeners that listening has stopped
                            JSObject ret = new JSObject();
                            ret.put("status", "stopped");
                            SpeechRecognition.this.notifyListeners(LISTENING_EVENT, ret);
                        }
                    } catch (Exception ex) {
                        throw ex;
                    } finally {
                        SpeechRecognition.this.lock.unlock();
                    }
                }
            );
    }

@sfcheng
Copy link
Author

sfcheng commented Aug 21, 2024

Has this fix been implemented?

@GerardoTovar
Copy link

GerardoTovar commented Aug 21, 2024

Has this fix been implemented?

No, for the moment I created a fork with the pull requests of others.
"@capacitor-community/speech-recognition": "github:GerardoTovar/speech-recognition#v6.0.2"
Let's hope they implement it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants