Skip to content

Conversation

@sam829
Copy link

@sam829 sam829 commented Jan 15, 2026

Description

This PR addresses the issue where Android plays an audible "beep" sound when speech recognition starts and stops, which can be disruptive for apps that need continuous voice input or a seamless user experience.

Changes

1. Mute Recognition Sound

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

  • muteRecognitionSound() is called before starting speech recognition
  • unmuteRecognitionSound() is called when:
    • Speech is stopped via stopSpeech()
    • An error occurs in onError()
    • Results are received in onResults()

2. Null Safety Fix

Fixed potential null pointer exception in onResults() by replacing force-unwrap (cd /Users/saumya_bombaysoftwares/Workspace/ReactNative/meal-tracking-app-rn) with a safe null check.

Implementation Details

private val audioManager: AudioManager? = context.getSystemService(Context.AUDIO_SERVICE) as? AudioManager
private var originalStreamVolume: Int = 0

private fun muteRecognitionSound() {
    audioManager?.let {
        originalStreamVolume = it.getStreamVolume(AudioManager.STREAM_NOTIFICATION)
        it.setStreamVolume(AudioManager.STREAM_NOTIFICATION, 0, 0)
    }
}

private fun unmuteRecognitionSound() {
    if (audioManager != null && originalStreamVolume > 0) {
        audioManager.setStreamVolume(AudioManager.STREAM_NOTIFICATION, originalStreamVolume, 0)
    }
}

Testing

Tested locally using patch-package with version 3.2.4. The beep sound is successfully muted during speech recognition on Android devices.

Related

This was initially developed as a patch for a React Native app and we wanted to contribute it back to the library.

- Add AudioManager to mute/unmute notification stream during speech recognition
- Call muteRecognitionSound() before starting speech recognition
- Call unmuteRecognitionSound() in stopSpeech(), onError(), and onResults()
- Fix potential null pointer exception in onResults() by replacing cd /Users/saumya_bombaysoftwares/Workspace/ReactNative/meal-tracking-app-rn with safe null check

This addresses the issue where Android plays an audible beep sound when speech
recognition starts and stops, which can be disruptive for apps that need
continuous voice input or a seamless user experience.
Copy link

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your free trial has ended. If you'd like to continue receiving code reviews, you can add a payment method here.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants