react-native-voice / voice

:microphone: React Native Voice Recognition library for iOS and Android (Online and Offline Support)
MIT License
1.77k stars 478 forks source link

Remove Android BIP sound on start and stop recognizing #48

Open MacKentoch opened 6 years ago

MacKentoch commented 6 years ago

In Android SpeechRecognizer emits (horrible) BIP sound when starting and ending recognition.

This by design but it would be awesome to be able to remove / mute these bips.

xavicolomer commented 6 years ago

Any news on that @MacKentoch ?

I would also like to remove that sound.

Noitidart commented 6 years ago

This would be a nice option.

ohtangza commented 6 years ago

@ghsdh3409 Ping. Time to sync with upstream?

alinematic commented 6 years ago

have u been able to do that?

dalvallana commented 5 years ago

I (big) while ago, I made a plugin for Cordova, and came up with a dirty hack to turn the volume off while recognizing and turn it back on when done. It's not the best solution but it works fine for now. I don't know exactly how to implement it now because it's been ages since I work with android, but just for the heck of it, here's the code of that class: https://github.com/dalvallana/ContinuousSpeechRecognizer/blob/master/src/android/ContinuousSpeechRecognizer.java. Look for the mStreamVolume variable.

roysG commented 5 years ago

any update?

Saltan77 commented 5 years ago

I also need such a function

imyagnesh commented 5 years ago

any update?

ReJaimes commented 5 years ago

Any update on this??

roysG commented 5 years ago

any Updateee?

subhendukundu commented 5 years ago

Any update?

ducpt2 commented 5 years ago

+1

vaibhgupta09 commented 4 years ago

any update

lfoliveir4 commented 4 years ago

@MacKentoch Did you solved your problem?

vaibhgupta09 commented 4 years ago

Hello Guyz i found a Hack to do this just simply set your phone volume to 0 before listening start. https://www.npmjs.com/package/react-native-system-setting check this out

MacKentoch commented 4 years ago

@lfoliveir4 no but try @vaibhgupta09 hack.

This is by design in Android: recording audio triggers a BIP sound -system does it - (should be related to legal reasons I guess).

So hacking is the only way to bypass as far as I know

Doodidan commented 4 years ago

I'm not sure it's good idea to avoid that sound. This makes user to know about listening and as far as this library can start listen without user action it's better to keep BIP sound turned on.

zombie6888 commented 4 years ago

But it would be good if I could change that sound. I've seen solution for Android API. Maybe I will try it later

Jonjoe commented 3 years ago

bump

phongbksneep commented 3 years ago

wait this option

Hirurgo commented 3 years ago

any update?

abhineetsharmathegreat commented 3 years ago

Any updates?

Doodidan commented 3 years ago

Down vote I really don't want this option exist in any RN project. It's not intuitively way to interact with client.

erevos-13 commented 6 months ago

Navigate to this file in: node_modules/@react-native-voice/voice/android/src/main/java/com/wenkesj/voice/VoiceModule.java

And there you can use the AUDIO_SERVICE to manipulate the sound on the mobile on android so when the microphone is on silent the phone and when is done to return to the volume levels that the user has. here a sample that i have try and work.

The patch encompasses the implementation of an Android-based speech recognition feature in a React Native application. It allows the application to mute background sound or music while the Speech Recognizer is capturing user speech. This has been achieved by employing the AudioManager, a core Android service that handles audio management, including volume and mode.

At the start of the VoiceModule class, a private field for AudioManager and an integer field for StreamVolume is declared. The AudioManager is later used to manage audio properties when the microphone is active.

private AudioManager mAudioManager = null;
private int mStreamVolume = 0;

The application stores the current music stream volume in the mStreamVolume variable when the VoiceModule object is created. This original volume level is maintained for restoration once the speech recognition operation is completed.

AudioManager mAudioManager = (AudioManager) this.reactContext.getSystemService(Context.AUDIO_SERVICE);
mStreamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);

When speech recognition begins, the application mutes the music stream volume. This ensures that the recognition process is not affected by any background music that might be playing.

muteStreamVolume();
speech.startListening(intent);

After the startListening() function is called, the muteStreamVolume() function mutes the audio stream only if the original volume wasn't 0.

private void muteStreamVolume() {
    if(mStreamVolume == 0) {
      return;
    }
    AudioManager mAudioManager = (AudioManager) this.reactContext.getSystemService(Context.AUDIO_SERVICE);
    mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 0, 0);
}

As soon as the recognition process is completed, the audio stream volume is restored to its original value. This is done in the onResults() method which is called when speech recognition results are ready.

setStreamVolumeBack();

The setStreamVolumeBack() function restores the original audio stream volume when called.

private void setStreamVolumeBack() {
    AudioManager mAudioManager = (AudioManager) this.reactContext.getSystemService(Context.AUDIO_SERVICE);
    mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, mStreamVolume, 0);
}

In conclusion, this patch significantly improves the user experience by ensuring that a user's voice command is not drowned out by background music during a voice-activated operation. This subtle yet essential modification enables users to interact more seamlessly with voice-activated applications and commands.

IMPORTANT After you edit the library you need to patch the use this tool:https://www.npmjs.com/package/patch-package search on the net to find out how is this done.