app/src/main/java/com/example/jarvis_stts/JarvisService.kt hinzugefügt
This commit is contained in:
66
app/src/main/java/com/example/jarvis_stts/JarvisService.kt
Normal file
66
app/src/main/java/com/example/jarvis_stts/JarvisService.kt
Normal file
@@ -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
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user