app/src/main/java/com/example/jarvis_stts/MainActivity.kt aktualisiert
This commit is contained in:
@@ -45,6 +45,23 @@ class MainActivity : AppCompatActivity(), TextToSpeech.OnInitListener {
|
||||
}
|
||||
}
|
||||
|
||||
private val speechRecognizerLauncher = registerForActivityResult(
|
||||
ActivityResultContracts.StartActivityForResult()
|
||||
) { result ->
|
||||
// Wenn Google fertig ist, starten wir das Wake-word wieder
|
||||
try {
|
||||
porcupineManager?.start()
|
||||
} catch (e: Exception) {
|
||||
Log.e("JARVIS", "Neustart nach Spracheingabe fehlgeschlagen")
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
setContentView(R.layout.activity_main)
|
||||
@@ -88,7 +105,7 @@ class MainActivity : AppCompatActivity(), TextToSpeech.OnInitListener {
|
||||
ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.RECORD_AUDIO), 1)
|
||||
} else {
|
||||
// App hat die Erlaubnis schon (z.B. beim zweiten App-Start) -> Direkt starten!
|
||||
initWakeWord()
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -105,6 +122,22 @@ class MainActivity : AppCompatActivity(), TextToSpeech.OnInitListener {
|
||||
}
|
||||
}
|
||||
|
||||
override fun onResume() {
|
||||
super.onResume()
|
||||
// Nur starten, wenn wir die Erlaubnis haben
|
||||
if (ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED) {
|
||||
// Falls der Manager schon läuft, stoppen wir ihn kurz, um ihn sauber neu zu starten
|
||||
porcupineManager?.stop()
|
||||
initWakeWord()
|
||||
}
|
||||
}
|
||||
|
||||
override fun onPause() {
|
||||
super.onPause()
|
||||
// Mikrofon pausieren, wenn die App nicht im Fokus ist, um den Fehler zu vermeiden
|
||||
porcupineManager?.stop()
|
||||
}
|
||||
|
||||
private fun connectToServer(url: String) {
|
||||
webSocket?.close(1000, "Neuverbindung")
|
||||
val request = Request.Builder().url(url).build()
|
||||
@@ -175,32 +208,35 @@ class MainActivity : AppCompatActivity(), TextToSpeech.OnInitListener {
|
||||
|
||||
private fun initWakeWord() {
|
||||
try {
|
||||
// HIER DEINEN PICOVOICE ACCESS KEY EINTRAGEN:
|
||||
val accessKey = "DEIN_KOPIERTER_PICOVOICE_KEY"
|
||||
val accessKey = "DEIN_KEY_HIER" // Bitte nochmal prüfen!
|
||||
|
||||
porcupineManager = PorcupineManager.Builder()
|
||||
.setAccessKey(accessKey)
|
||||
// Du kannst hier auch .COMPUTER, .BUMBLEBEE oder .PORCUPINE wählen
|
||||
.setKeyword(Porcupine.BuiltInKeyword.JARVIS)
|
||||
.build(applicationContext, PorcupineManagerCallback { keywordIndex ->
|
||||
if (keywordIndex == 0) {
|
||||
// Wake-Word wurde erkannt!
|
||||
runOnUiThread {
|
||||
tvStatus.text = "Wake-Word erkannt! Höre zu..."
|
||||
// Startet sofort die Google-Spracherkennung für deinen Befehl
|
||||
// 1. Wake-word stoppen, damit Google das Mikrofon kriegt
|
||||
porcupineManager?.stop()
|
||||
// 2. Google-Eingabe starten
|
||||
startVoiceInput()
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
// Startet das Dauerlauschen im Hintergrund
|
||||
porcupineManager?.start()
|
||||
Log.d("WakeWord", "Porcupine lauscht auf 'Jarvis'...")
|
||||
Log.d("JARVIS", "Porcupine erfolgreich gestartet")
|
||||
|
||||
} catch (e: Exception) {
|
||||
Log.e("WakeWord", "Fehler beim Starten von Porcupine: ${e.message}")
|
||||
// DAS HIER IST WICHTIG: Was genau ist der Fehler?
|
||||
val errorMessage = when (e) {
|
||||
is ai.picovoice.porcupine.PorcupineActivationException -> "Key ungültig oder Limit erreicht"
|
||||
is ai.picovoice.porcupine.PorcupineActivationNetworkException -> "Keine Internetverbindung zur Key-Prüfung"
|
||||
is ai.picovoice.porcupine.PorcupineInvalidArgumentException -> "Falsches Keyword oder Argument"
|
||||
else -> e.message ?: "Unbekannter Fehler"
|
||||
}
|
||||
|
||||
Log.e("JARVIS", "Detail-Fehler: $errorMessage")
|
||||
runOnUiThread {
|
||||
Toast.makeText(this, "Wake-Word Fehler: ${e.message}", Toast.LENGTH_LONG).show()
|
||||
tvStatus.text = "Fehler: $errorMessage"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user