69 lines
2.1 KiB
Kotlin
69 lines
2.1 KiB
Kotlin
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
|
|
import java.io.File
|
|
|
|
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 {
|
|
// MainActivity entpackt nach "model", also greifen wir hier darauf zu:
|
|
val modelPath = File(filesDir, "model").absolutePath
|
|
|
|
val model = Model(modelPath)
|
|
|
|
// WICHTIG: Nutze hier "computer" ODER "jarvis",
|
|
// je nachdem was du in der MainActivity definiert hast.
|
|
val recognizer = Recognizer(model, 16000f, "[\"computer\", \"jarvis\", \"[unk]\"]")
|
|
|
|
speechService = SpeechService(recognizer, 16000f)
|
|
speechService?.startListening(this)
|
|
Log.d("JARVIS", "Service: Vosk hört jetzt zu...")
|
|
|
|
} catch (e: Exception) {
|
|
Log.e("JARVIS", "Service: Fehler beim Laden des Modells: ${e.message}")
|
|
}
|
|
}
|
|
|
|
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
|
|
} |