app/src/main/java/com/example/jarvis_stts/MainActivity.kt aktualisiert
This commit is contained in:
@@ -45,7 +45,6 @@ class MainActivity : AppCompatActivity(), RecognitionListener, TextToSpeech.OnIn
|
|||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
setContentView(R.layout.activity_main)
|
setContentView(R.layout.activity_main)
|
||||||
|
|
||||||
// 1. UI initialisieren
|
|
||||||
tvStatus = findViewById(R.id.tvStatus)
|
tvStatus = findViewById(R.id.tvStatus)
|
||||||
etUrl = findViewById(R.id.etUrl)
|
etUrl = findViewById(R.id.etUrl)
|
||||||
spinnerVoices = findViewById(R.id.spinnerVoices)
|
spinnerVoices = findViewById(R.id.spinnerVoices)
|
||||||
@@ -54,7 +53,6 @@ class MainActivity : AppCompatActivity(), RecognitionListener, TextToSpeech.OnIn
|
|||||||
|
|
||||||
tts = TextToSpeech(this, this)
|
tts = TextToSpeech(this, this)
|
||||||
|
|
||||||
// 2. SharedPreferences
|
|
||||||
val prefs = getSharedPreferences("JarvisPrefs", MODE_PRIVATE)
|
val prefs = getSharedPreferences("JarvisPrefs", MODE_PRIVATE)
|
||||||
etUrl.setText(prefs.getString("server_url", ""))
|
etUrl.setText(prefs.getString("server_url", ""))
|
||||||
|
|
||||||
@@ -66,9 +64,11 @@ class MainActivity : AppCompatActivity(), RecognitionListener, TextToSpeech.OnIn
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
btnSpeak.setOnClickListener { startVoiceInput() }
|
btnSpeak.setOnClickListener {
|
||||||
|
voskService?.stop() // Vosk stoppen, wenn man manuell klickt
|
||||||
|
startVoiceInput()
|
||||||
|
}
|
||||||
|
|
||||||
// 3. Vosk Modell laden & Berechtigungen
|
|
||||||
if (ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {
|
if (ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {
|
||||||
ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.RECORD_AUDIO), 1)
|
ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.RECORD_AUDIO), 1)
|
||||||
} else {
|
} else {
|
||||||
@@ -76,6 +76,14 @@ class MainActivity : AppCompatActivity(), RecognitionListener, TextToSpeech.OnIn
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// KORREKTUR: Damit Jarvis sofort nach der Erlaubnis startet
|
||||||
|
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
|
||||||
|
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
|
||||||
|
if (requestCode == 1 && grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
|
||||||
|
initVoskModel()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private fun initVoskModel() {
|
private fun initVoskModel() {
|
||||||
StorageService.unpack(this, "model-de", "model",
|
StorageService.unpack(this, "model-de", "model",
|
||||||
{ model: Model ->
|
{ model: Model ->
|
||||||
@@ -91,12 +99,14 @@ class MainActivity : AppCompatActivity(), RecognitionListener, TextToSpeech.OnIn
|
|||||||
private fun startVosk() {
|
private fun startVosk() {
|
||||||
try {
|
try {
|
||||||
if (voskModel == null) return
|
if (voskModel == null) return
|
||||||
// Wir horchen auf "jarvis". [unk] lässt unbekannte Wörter zu.
|
// Falls noch ein alter Service läuft, sicherheitshalber stoppen
|
||||||
val rec = Recognizer(voskModel, 16000.0f, "[\"computer\", \"[unk]\"]")
|
voskService?.stop()
|
||||||
//val rec = Recognizer(voskModel, 16000.0f, "[\"jarvis\", \"[unk]\"]")
|
voskService?.shutdown()
|
||||||
|
|
||||||
|
val rec = Recognizer(voskModel, 16000.0f, "[\"computer\", \"jarvis\", \"[unk]\"]")
|
||||||
voskService = SpeechService(rec, 16000.0f)
|
voskService = SpeechService(rec, 16000.0f)
|
||||||
voskService?.startListening(this)
|
voskService?.startListening(this)
|
||||||
runOnUiThread { tvStatus.text = "Bereit (Warte auf 'Jarvis')" }
|
runOnUiThread { tvStatus.text = "Bereit (Warte auf 'Computer')" }
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
Log.e("JARVIS", "Vosk Start Fehler: ${e.message}")
|
Log.e("JARVIS", "Vosk Start Fehler: ${e.message}")
|
||||||
}
|
}
|
||||||
@@ -111,8 +121,9 @@ class MainActivity : AppCompatActivity(), RecognitionListener, TextToSpeech.OnIn
|
|||||||
val recognizedText = extractText(hypothesis)
|
val recognizedText = extractText(hypothesis)
|
||||||
Log.d("JARVIS", "Vosk hört: $recognizedText")
|
Log.d("JARVIS", "Vosk hört: $recognizedText")
|
||||||
|
|
||||||
if (recognizedText.contains("computer") || recognizedText.contains("jarvis")) {
|
// KORREKTUR: ignoreCase hinzugefügt für mehr Sicherheit
|
||||||
voskService?.stop() // Pause statt Stop ist oft schneller
|
if (recognizedText.contains("computer", true) || recognizedText.contains("jarvis", true)) {
|
||||||
|
voskService?.stop()
|
||||||
startVoiceInput()
|
startVoiceInput()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -126,8 +137,9 @@ class MainActivity : AppCompatActivity(), RecognitionListener, TextToSpeech.OnIn
|
|||||||
tvStatus.text = "Ich: $spokenText"
|
tvStatus.text = "Ich: $spokenText"
|
||||||
webSocket?.send(spokenText)
|
webSocket?.send(spokenText)
|
||||||
}
|
}
|
||||||
// WICHTIG: Warte kurz oder prüfe, ob TTS spricht, bevor du das hier machst:
|
|
||||||
voskService?.startVosk()
|
// KORREKTUR: Einfach die Funktion aufrufen, nicht über voskService
|
||||||
|
startVosk()
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onResult(hypothesis: String) {}
|
override fun onResult(hypothesis: String) {}
|
||||||
|
|||||||
Reference in New Issue
Block a user