0

Im building an app that translate audio in background on live. I display a overlay over apps and i want to record the audio to translate in real time. But in android 13 the audio recording stops after a few secods recording. I guess that the problem is for the security of the user, but im looking for a second opinion

Im using the SpeechRecognizer library. But I receive recommendations

Added a piece of code

AndroidManifiest.xml

<uses-permission android:name="android.permission.RECORD_AUDIO" />

<service
            android:name=".feature.overlay.OverlayService"
            android:exported="false"
            android:foregroundServiceType="microphone"/>

OverlayService

override fun onCreate() {
        super.onCreate()

        windowManager = getSystemService(Context.WINDOW_SERVICE) as WindowManager
        configureSpeechToText()

        windowManager.addView(layoutText,params)

    }

fun configureSpeechToText() {
        speechRecognizer = SpeechRecognizer.createSpeechRecognizer(this)
        speechRecognizerIntent.putExtra(
            RecognizerIntent.EXTRA_LANGUAGE_MODEL,
            RecognizerIntent.LANGUAGE_MODEL_FREE_FORM
        )
        speechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault())
        speechRecognizer?.setRecognitionListener(object : RecognitionListener {
            override fun onReadyForSpeech(bundle: Bundle) {}
            override fun onBeginningOfSpeech() {}
            override fun onRmsChanged(v: Float) {}
            override fun onBufferReceived(bytes: ByteArray) {}
            override fun onEndOfSpeech() {}
            override fun onError(i: Int) {
                val errorMessage = getErrorText(i)
                Log.d("Error", "FAILED: $errorMessage")
            }
            override fun onResults(bundle: Bundle) {
                val data = bundle.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION)
                dataVoiceText = data!![0]
                run(dataVoiceText)//The function that translate the text
            }

            override fun onPartialResults(bundle: Bundle) {}
            override fun onEvent(i: Int, bundle: Bundle) {}
        })

        handler.postDelayed(Runnable {//Run this code every few seconds to translate every few seconds
            handler.postDelayed(runnable!!, delay.toLong())
            speechRecognizer?.stopListening()
            speechRecognizer?.startListening(speechRecognizerIntent)
        }.also { runnable = it }, delay.toLong())
    }

Im trying to record audio in background with a service, im expecting to it continously record the audio. but right now it is stoping

  • Does your use of `SpeechRecognizer` work from a more conventional place, like an `Activity`? In other words, is `SpeechRecognizer` itself designed for your scenario? [The documentation](https://developer.android.com/reference/android/speech/SpeechRecognizer) states "this API is not intended to be used for continuous recognition, which would consume a significant amount of battery and bandwidth. " – CommonsWare Feb 17 '23 at 00:31
  • @CommonsWare it works from an activity, but im looking for a solution to continously recognition, even if i have to use another library – Valeria Henao Feb 17 '23 at 00:36
  • 1
    Are you aware of [Doze mode](https://developer.android.com/training/monitoring-device-state/doze-standby#restrictions) which will when the device is physically idle ignore [wake locks](https://developer.android.com/training/scheduling/wakelock) and therefore stop your service from running? – Morrison Chang Feb 17 '23 at 01:06
  • Looks like you're missing some permissions. See [How to Give Screen Overlay Permission On My Activity](https://stackoverflow.com/questions/40437721/how-to-give-screen-overlay-permission-on-my-activity) – Rob Feb 18 '23 at 08:55

0 Answers0