Skip to content

[Android] Add option to mute speech recognition beep sound + NativeEventEmitter warning fix #585

@sam829

Description

@sam829

Description

When using @react-native-voice/voice v3.2.4 on Android, there are several issues:

1. Speech Recognition Beep Sound

Android plays an audible "beep" sound when speech recognition starts and stops. This is disruptive for many use cases, especially in apps that need continuous voice input or a seamless user experience.

2. NativeEventEmitter Warning

React Native emits a warning about requiring addListener and removeListeners methods on the native module:
WARN new NativeEventEmitter() was called with a non-null argument without the required addListener method. WARN new NativeEventEmitter() was called with a non-null argument without the required removeListeners method.

3. Null pointer risk in onPartialResults/onResults

The results Bundle from the speech recognizer can potentially be null, but the code doesn't check for this before processing.

Proposed Solution

Mute Recognition Sound

Add muteRecognitionSound() and unmuteRecognitionSound() methods that temporarily mute the STREAM_NOTIFICATION during speech recognition:

private AudioManager mAudioManager = null;
private int mStreamVolume = 0;
private void muteRecognitionSound() {
    if (mAudioManager == null) return;
    mStreamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_NOTIFICATION);
    mAudioManager.setStreamVolume(AudioManager.STREAM_NOTIFICATION, 0, 0);
}
private void unmuteRecognitionSound() {
    if (mAudioManager == null || mStreamVolume == 0) return;
    mAudioManager.setStreamVolume(AudioManager.STREAM_NOTIFICATION, mStreamVolume, 0);
}

Call muteRecognitionSound() before starting recognition and unmuteRecognitionSound() when:

Recognition completes successfully (onResults)
Recognition is stopped (stopSpeech)
An error occurs (onError)

Fix NativeEventEmitter Warning

Add stub methods to the Voice native module in dist/index.js:

if (Voice) { 
  Voice.addListener = Voice.addListener || (() => {}); 
  Voice.removeListeners = Voice.removeListeners || (() => {}); 
}

Add Null Safety

Check if results is null before processing in onPartialResults() and onResults().

Extended Silence Timeout

Optionally, increase the default values for speech input silence detection to allow for longer pauses between words:

EXTRA_SPEECH_INPUT_COMPLETE_SILENCE_LENGTH_MILLIS: 15000,
EXTRA_SPEECH_INPUT_POSSIBLY_COMPLETE_SILENCE_LENGTH_MILLIS: 15000,

Environment

Library version: 3.2.4
React Native version: 0.79+
Platform: Android

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions