The problem is that the voice assistant I made doesn't work. The application itself starts, but when the button is pressed, the microphone does not work, and when it is turned off (pressed again), there are sounds of instant on and off. I believe the problem lies in this code snippet:
if (status == TextToSpeech.SUCCESS) {
val result: Int = textToSpeech.setLanguage(Locale.ENGLISH)
if (result == TextToSpeech.LANG_MISSING_DATA ||
result == TextToSpeech.LANG_NOT_SUPPORTED
) {
Log.e(logtts, "Language not supported")
} else {
Log.e(logtts, "Language supported")
}
} else {
Log.e(logtts, "Initialization failed")
}
}
speechRecognizer = SpeechRecognizer.createSpeechRecognizer(this)
recognizerIntent = Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH)
recognizerIntent.putExtra(
RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM
)
recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault())
speechRecognizer.setRecognitionListener(object : RecognitionListener {
override fun onReadyForSpeech(p0: Bundle?) {
}
override fun onBeginningOfSpeech() {
Log.d("SR", "started")
}
override fun onRmsChanged(rmsdB: Float) {
}
override fun onBufferReceived(buffer: ByteArray?) {
TODO("Not yet implemented")
}
override fun onEndOfSpeech() {
Log.d("SR", "ended")
}
override fun onError(error: Int) {
TODO("Not yet implemented")
}
@RequiresApi(Build.VERSION_CODES.O)
override fun onResults(bundle: Bundle) {
val data = bundle.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION)
if (data != null) {
keeper = data[0]
Log.d(logkeeper, keeper)
when {
keeper.contains("thanks") -> speak("Its my job")
keeper.contains("welcome") -> speak("Hello Wassup bitch")
keeper.contains("clear") -> assistantViewModel.Onclear()
keeper.contains("date") -> getDate()
keeper.contains("time") -> getTime()
keeper.contains("sens SMS") -> sendSMS()
keeper.contains("read my last SMS") -> readSMS()
keeper.contains("phone call") -> makeAPhoneCall()
keeper.contains("open Gmail") -> openGmail()
keeper.contains("open WhatsApp") -> openWhatsApp()
keeper.contains("open Facebook") -> openFacebook()
keeper.contains("open messages") -> openMessages()
keeper.contains("share a file") -> shareAFile()
keeper.contains("share a text message") -> shareATextFile()
keeper.contains("call") -> callContact()
keeper.contains("turn on Bluetooth") -> turnOnBluetooth()
keeper.contains("turn off Bluetooth") -> turnOffBluetooth()
keeper.contains("get Bluetooth devices") -> getAllPairedDevice()
keeper.contains("turn on flash") -> turnOnFlash()
keeper.contains("turn off flash") -> turnOffFlash()
keeper.contains("copy to clipboard") -> clipBoardCopy()
keeper.contains("read last clipboard") -> clipBoardSpeak()
keeper.contains("capture photo") -> capturePhoto()
keeper.contains("play ringtone") -> playRingtone()
keeper.contains("stop ringtone") -> stopRingtone()
keeper.contains("read me") -> readMe()
keeper.contains("alarm") -> setAlarm()
keeper.contains("weather") -> weather()
keeper.contains("horoscope") -> horoscope()
keeper.contains("medical") -> medicalApplication()
keeper.contains("joke") -> joke()
keeper.contains("question") -> question()
keeper.contains("hello") -> speak("Hello, can i help you?")
}
}
}
override fun onPartialResults(partialResults: Bundle?) {
TODO("Not yet implemented")
}
override fun onEvent(p0: Int, p1: Bundle?) {
TODO("Not yet implemented")
}
})
binding.assistantActionButton.setOnTouchListener { view, motionEvent ->
when (motionEvent.action) {
MotionEvent.ACTION_UP -> {
speechRecognizer.stopListening()
}
MotionEvent.ACTION_DOWN -> {
textToSpeech.stop()
speechRecognizer.startListening(recognizerIntent)
}
}
false
}
checkIfSpeechRecognizerAvaible()
}
private fun checkIfSpeechRecognizerAvaible() {
if(SpeechRecognizer.isRecognitionAvailable(this)){
Log.d(logsr,"yes")
}
else
{
Log.d(logsr,"false")
}
}
fun speak(text: String){
textToSpeech.speak(text, TextToSpeech.QUEUE_FLUSH,null,"")
assistantViewModel.sendMessageToDatabase(keeper,text)
}
I ask you to help with this problem, as the project will be handed over in two weeks. If necessary, I can provide the whole project)