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

本文介绍如何在Android应用中使用Microsoft Azure的认知服务SDK进行语音到文本的转换。通过设置正确的订阅密钥和服务区域,可以调整语言和文化,实现不同语言的语音识别。文章还提供了如何开始和停止连续识别,以及如何在识别过程中获取实时结果的示例代码。
  • 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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值