From 20fafd40ddc8855ee00f35b5902a8de5d6bfc37d Mon Sep 17 00:00:00 2001 From: "info@pi-farm.de" Date: Wed, 11 Mar 2026 14:08:40 +0000 Subject: [PATCH] =?UTF-8?q?app/src/main/java/com/example/jarvis=5Fstts/Jar?= =?UTF-8?q?visService.kt=20hinzugef=C3=BCgt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/example/jarvis_stts/JarvisService.kt | 66 +++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 app/src/main/java/com/example/jarvis_stts/JarvisService.kt diff --git a/app/src/main/java/com/example/jarvis_stts/JarvisService.kt b/app/src/main/java/com/example/jarvis_stts/JarvisService.kt new file mode 100644 index 0000000..e2ef9e0 --- /dev/null +++ b/app/src/main/java/com/example/jarvis_stts/JarvisService.kt @@ -0,0 +1,66 @@ +package com.example.jarvis_stts + +import android.app.Service +import android.content.Intent +import android.os.IBinder +import org.vosk.Model +import org.vosk.Recognizer +import org.vosk.android.SpeechService +import org.vosk.android.RecognitionListener +import java.io.IOException + +class JarvisService : Service(), RecognitionListener { + + private var speechService: SpeechService? = null + + override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { + setupVosk() + return START_STICKY // Sorgt dafür, dass der Service bei Beendung neu startet + } + + private fun setupVosk() { + try { + // Der Pfad muss zum Ordner zeigen, in den du das Modell entpackt hast + val modelPath = getExternalFilesDir(null)?.absolutePath + "/model-de" + val model = Model(modelPath) + + // WICHTIG: Wir limitieren den Recognizer auf das Wake-Word "jarvis" + // Das erhöht die Erkennungsrate massiv! + val recognizer = Recognizer(model, 16000f, "[\"jarvis\", \"[unk]\"]") + + speechService = SpeechService(recognizer, 16000f) + speechService?.startListening(this) + + } catch (e: IOException) { + e.printStackTrace() + } + } + + override fun onResult(hypothesis: String?) { + // hypothesis ist ein JSON String, z.B.: { "text" : "jarvis" } + if (hypothesis != null && hypothesis.contains("jarvis")) { + println("WAKE WORD ERKANNT!") + // Hier triggerst du deine Antwort-Logik + } + } + + override fun onPartialResult(hypothesis: String?) { + // Wird während des Sprechens aufgerufen + } + + override fun onFinalResult(hypothesis: String?) {} + + override fun onError(e: Exception?) { + e.printStackTrace() + } + + override fun onTimeout() {} + + override fun onDestroy() { + super.onDestroy() + speechService?.stop() + speechService?.shutdown() + } + + override fun onBind(intent: Intent?): IBinder? = null +} \ No newline at end of file