I want to build an app that constantly listens for a keyword and when it finds this keyword, the keyword should do something in the UI.
I use speechRecoginzer but there is a problem that app only listen for several seconds and I want to build app which listen all the time until app is destroy. I make it in background service. There is a better class to make class like this. I look for many solution and can't find solution which can constantly listen. Here in my code my app invoke startListening() ale the time but the problem is that if user have loud mode on phone it will be irrating voice of activating listening by phone every time when is invoke. Thanks in advance. There is my code:
class AudioListenerService : Service() {
private var speechRecognizer: SpeechRecognizer? = null
private var flag:Boolean = true
override fun onBind(intent: Intent?): IBinder? {
return null
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
// Wyłącz dźwięk powiadomień
/*
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.setInterruptionFilter(NotificationManager.INTERRUPTION_FILTER_NONE)
*/
startListening()
return START_STICKY
}
private fun startListening() {
speechRecognizer = SpeechRecognizer.createSpeechRecognizer(this)
val recognizerIntent = Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH)
recognizerIntent.putExtra(
RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM
)
recognizerIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, packageName)
speechRecognizer?.setRecognitionListener(object : RecognitionListener {
override fun onReadyForSpeech(params: Bundle?) {
Log.d("AudioListenerService", "Ready for speech")
}
override fun onBeginningOfSpeech() {
Log.d("AudioListenerService", "Speech began")
}
override fun onRmsChanged(rmsdB: Float) {}
override fun onBufferReceived(buffer: ByteArray?) {}
override fun onEndOfSpeech() {
Log.d("AudioListenerService", "End of speech")
}
override fun onError(error: Int) {
Log.e("AudioListenerService", "Recognition error: $error")
// Jeśli wystąpił błąd rozpoznawania, uruchamiamy nasłuchiwanie ponownie
startListening()
}
override fun onPartialResults(partialResults: Bundle?) {}
override fun onEvent(eventType: Int, params: Bundle?) {}
override fun onResults(results: Bundle?) {
val matches = results?.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION)
if (matches != null) {
for (result in matches) {
Log.d("MYAPP", result)
if (result.contains("stop", ignoreCase = true)) {
// Tutaj możesz wywołać odpowiednią akcję, gdy słowo kluczowe zostanie rozpoznane
flag=false
break
}
}
}
// Uruchamiamy nasłuchiwanie ponownie
if(flag){
startListening()
}
}
})
speechRecognizer?.startListening(recognizerIntent)
}
private fun stopListening() {
speechRecognizer?.stopListening()
speechRecognizer?.cancel()
speechRecognizer?.destroy()
speechRecognizer = null
}
override fun onCreate() {
super.onCreate()
startListening() // Uruchom nasłuchiwanie przy uruchamianiu usługi
}
override fun onDestroy() {
super.onDestroy()
speechRecognizer?.destroy() // Zatrzymaj nasłuchiwanie przy zniszczeniu usługi
}
}