I'm working with android speech API called SpeechRecognizer trying to translate speech into text, but for some reason, as soon as I click on the button - I see the message "CAN'T REACH GOOGLE AT THE MOMENT", and the window closes without waiting for my speech. Or the sound just stutters and nothing happens.
class MainActivity : AppCompatActivity() {
var voiceButton: Button? = null
var textView: TextView? = null
var listener: Listener? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
initializeView()
initializeListeners()
}
fun initializeView() {
voiceButton = findViewById(R.id.voiceButton)
textView = findViewById(R.id.textView)
listener = Listener()
}
fun initializeListeners() {
voiceButton?.setOnClickListener {
val speechRecognizer = SpeechRecognizer.createSpeechRecognizer(this)
speechRecognizer.setRecognitionListener(listener)
val intent = Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH)
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM)
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault())
intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS,5);
speechRecognizer.startListening(intent)
if (intent.resolveActivity(packageManager) != null) {
startActivityForResult(intent, 10)
} else {
Toast.makeText(this, "Unfortunately device not supported", Toast.LENGTH_SHORT).show()
}
}
}
Just why? What's wrong with this code? I think this code should work because for other people it works fine. Maybe my emulator (Nexus One API 22) is not supported for this API? I just don't know what's the problem.
Maybe you do know how to solve it? Thanks, i'll be grateful for your help!
IF SOMEBODY SUDDENLY FACES A SIMILAR PROBLEM, THAT'S HOW I SOLVE THIS PROBLEM AFTER A WHILE:
You need to turn on Virtual microphone uses host audio input. Go to: More ("..." on the right side) -> Microphone -> Turn on "Virtual microphone uses host audio input". Also, your emulator must have a later version of the update, otherwise speech recognition will not work.