How to implement Android TTS via MS Java SDK package (android) - Speech to Text

  • 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();
        }

    Btw: https://github.com/Azure-Samples/cognitive-services-speech-sdk/tree/master/samples/java/android/sdkdemo

Please click like, if you feel it's working on your project.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值