diff --git a/app/src/main/java/com/example/jarvis_stts/MainActivity.kt b/app/src/main/java/com/example/jarvis_stts/MainActivity.kt index 53dd5be..3232e6c 100644 --- a/app/src/main/java/com/example/jarvis_stts/MainActivity.kt +++ b/app/src/main/java/com/example/jarvis_stts/MainActivity.kt @@ -116,18 +116,34 @@ class MainActivity : AppCompatActivity(), RecognitionListener, TextToSpeech.OnIn } } - // --- Vosk RecognitionListener --- - override fun onPartialResult(hypothesis: String) { - // Diese Zeile zeigt dir live im Logcat, was Vosk gerade verstanden hat: - Log.d("JARVIS", "Vosk hört: $hypothesis") + // 1. Hilfsfunktion zum sauberen Filtern des Wortes + private fun extractText(json: String): String { + return json.substringAfter(": \"").substringBefore("\"") + } - // Testweise auf "computer" hören - if (hypothesis.contains("computer", ignoreCase = true)) { - voskService?.stop() + override fun onPartialResult(hypothesis: String) { + val recognizedText = extractText(hypothesis) + Log.d("JARVIS", "Vosk hört: $recognizedText") + + if (recognizedText.contains("computer") || recognizedText.contains("jarvis")) { + voskService?.pause() // Pause statt Stop ist oft schneller startVoiceInput() } } + // 2. Im SpeechRecognizerLauncher + private val speechRecognizerLauncher = registerForActivityResult( + ActivityResultContracts.StartActivityForResult() + ) { result -> + if (result.resultCode == RESULT_OK && result.data != null) { + val spokenText = result.data!!.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS)?.get(0) ?: "" + tvStatus.text = "Ich: $spokenText" + webSocket?.send(spokenText) + } + // WICHTIG: Warte kurz oder prüfe, ob TTS spricht, bevor du das hier machst: + voskService?.resume() + } + override fun onResult(hypothesis: String) {} override fun onFinalResult(hypothesis: String) {} override fun onError(e: Exception) { Log.e("JARVIS", "Vosk Error: ${e.message}") }