- Speech to Text package:
package com.microsoft.cognitiveservices.speech;
- Add following line in your [build.gradle - app]
implementation 'com.microsoft.cognitiveservices.speech:client-sdk:1.1.0'
-
in you specified project, you can add following code:
config = SpeechConfig.fromSubscription(speechSubscriptionKey, serviceRegion); assert (config != null); // adjust different language culture clientContext = JSON.parseObject(getSharedPreference(ConstString.CLIENT_CONTEXT, config.setSpeechRecognitionLanguage("zh-CN"); config.setOutputFormat(OutputFormat.Detailed); recognizer = new SpeechRecognizer(config);
Note that You must set proper recognition language before you created "SpeechRecognizer" instance. Otherwise, the language will not be changed. For speechSubScriptionKey, and service Region, please get it from Azure with your azure account. (SpeechRecognizer has default language "en-US")
-
If you are going to retrieve one time result. You can call following result.
recognizer.recognizeOnceAsync()
What if you want to get result while starting recognition, you can call
recognizer.startContinuousRecognitionAsync();
and apply event "recognizing" to retrieve runtime result.
recognizer.recognizing.addEventListener(new EventHandler<SpeechRecognitionEventArgs>() { @Override public void onEvent(Object o, SpeechRecognitionEventArgs speechRecognitionEventArgs) { text = speechRecognitionEventArgs.getResult().getText(); Log.d("recognizer.recognizing:", text); setText(text); } });
In the end, Please do not forget to call close method of recognizer object in your onDestory method of Activity.
@Override protected void onDestroy() { // in the end, recognizer need to close if (recognizer != null) recognizer.close(); super.onDestroy(); }
Please click like, if you feel it's working on your project.