Android learning - Speech Recognition

speech recognition

IFLYTEK open platform

Official website: http://www.xfyun.cn/

Development documents: https://doc.xfyun.cn/msc_android/index.html

Create application

Download SDK and select required functions

Import SDK

add permission

<! -- connect to network permission, used to perform cloud voice capability -- >
<uses-permission android:name="android.permission.INTERNET"/>
<! -- access to the use right of mobile recorder, which is required for dictation, recognition and semantic understanding -- >
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<! -- read network information status -- >
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<! -- get the current wifi status -- >
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<! -- allow programs to change network connection status -- >
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
<! -- access to mobile phone information -- >
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<! -- read contact permission, which is required to upload contacts -- >
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<! -- write permission for external storage, which is required for building syntax -- >
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<! -- read permission for external storage, which is required for building syntax -- >
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<! -- configuration permission, used to record application configuration information -- >
<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
<! -- mobile location information, used to provide location for semantic and other functions, and provide more accurate services -- >
<! -- location information is sensitive information. You can close the location request through Setting.setLocationEnable(false) - >
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<! -- if you need to use face recognition, you need to add: camera authority, and you need to use -- >
<uses-permission android:name="android.permission.CAMERA" />

Initialization

// Replace "12345678" with APPID you applied for at http://www.xfyun.cn
// Do not add any empty characters or escape characters between "=" and appid
SpeechUtility.createUtility(context, SpeechConstant.APPID +"=12345678");

voice dictation

package com.example.yuyin;

import android.os.IBinder;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.iflytek.cloud.RecognizerListener;
import com.iflytek.cloud.RecognizerResult;
import com.iflytek.cloud.SpeechConstant;
import com.iflytek.cloud.SpeechError;
import com.iflytek.cloud.SpeechRecognizer;
import com.iflytek.cloud.SpeechUtility;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.LinkedHashMap;


public class MainActivity extends AppCompatActivity {

    private TextView tv_content;
    private HashMap<String,String> nIatResults=new LinkedHashMap<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // Replace "12345678" with APPID you applied for at http://www.xfyun.cn
        // Do not add any empty characters or escape characters between "=" and appid
        SpeechUtility.createUtility(this, SpeechConstant.APPID +"=5ce4a75c");
        tv_content=findViewById(R.id.info);
    }

    //voice dictation
    public void startSpeachClick(View view){
        //Initialize recognition no UI recognition object
        //Using the SpeechRecognizer object, you can customize the interface according to the callback message;
        SpeechRecognizer mIat = SpeechRecognizer.createRecognizer(this, null);

        //Set the syntax ID and SUBJECT to null to avoid setting this parameter due to previous syntax calls, or clear all parameters directly. For details, refer to the example of DEMO.
        mIat.setParameter( SpeechConstant.DOMAIN, "iat" );//application area
        //Set the speech input language to simplified Chinese
        mIat.setParameter(SpeechConstant.LANGUAGE, "zh_cn");
        //Set result return language, Mandarin
        mIat.setParameter(SpeechConstant.ACCENT, "mandarin");
        mIat.setParameter(SpeechConstant.ENGINE_TYPE,SpeechConstant.TYPE_CLOUD);
        //Format return results
        mIat.setParameter(SpeechConstant.RESULT_TYPE,"json");
        //Set the voice front endpoint: Mute timeout, in ms, that is, how long the user does not speak is treated as timeout
        //Value range {1000 ~ 10000}
        mIat.setParameter(SpeechConstant.VAD_BOS, "4000");
        //Set the end point after voice: the mute detection time of the back-end point, in ms, that is, how long the user stops talking is considered as no longer input,
        //Auto stop recording, range {0 ~ 10000}
        mIat.setParameter(SpeechConstant.VAD_EOS, "1000");
        //Set punctuation to "0" to return results without punctuation and "1" to return results with punctuation
        mIat.setParameter(SpeechConstant.ASR_PTT,"1");
        //Set dynamic correction of dictation
        mIat.setParameter(SpeechConstant.ASR_DWA,"0");

        //Start recognition and set up a listener
        mIat.startListening(mRecogListener);
    }

    private RecognizerListener mRecogListener = new RecognizerListener() {
        //0-30 volume adjustment
        @Override
        public void onVolumeChanged(int i, byte[] bytes) {

        }
        //Start recording
        @Override
        public void onBeginOfSpeech() {

        }
        //Stop recording
        @Override
        public void onEndOfSpeech(){

        }

        @Override
        public void onError(SpeechError speechError) {

        }

        //Return result
        @Override
        public void onResult(RecognizerResult recognizerResult, boolean b) {
            Log.i("tag",recognizerResult.getResultString());
            printResult(recognizerResult);
        }

        @Override
        public void onEvent(int i, int i1, int i2, Bundle bundle) {

        }
    };

    private void printResult(RecognizerResult results){
        String text=JsonParser.parseGrammarResult(results.getResultString());
        String sn=null;
        //Read sn field in json result
        try {
            JSONObject resultJson=new JSONObject(results.getResultString());
            sn=resultJson.optString("sn");
        } catch (JSONException e) {
            e.printStackTrace();
        }

        nIatResults.put(sn,text);

        StringBuffer resultBuffer=new StringBuffer();
        for (String key:nIatResults.keySet()){
            resultBuffer.append(nIatResults.get(key));
        }
        tv_content.setText(resultBuffer.toString());
    }
}

speech synthesis

package com.example.yuyin;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

import com.iflytek.cloud.SpeechConstant;
import com.iflytek.cloud.SpeechError;
import com.iflytek.cloud.SpeechSynthesizer;
import com.iflytek.cloud.SpeechUtility;
import com.iflytek.cloud.SynthesizerListener;

public class Main2Activity extends AppCompatActivity {

    private EditText editText_content;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        SpeechUtility.createUtility(this, SpeechConstant.APPID +"=5ce4a75c");
        editText_content=findViewById(R.id.wenzi);
    }

    public void startClick(View view){
        //Create speech synthesizer
        SpeechSynthesizer mTts=SpeechSynthesizer.createSynthesizer(this,null);
        //Synthesis parameters
        mTts.setParameter(SpeechConstant.VOICE_NAME,"xiaoyan");
        mTts.setParameter(SpeechConstant.SPEED,"50");//Speech rate
        mTts.setParameter( SpeechConstant.VOLUME, "80");//volume
        mTts.setParameter( SpeechConstant.ENGINE_TYPE, SpeechConstant.TYPE_CLOUD );
        //Save path
//        mTts.setParameter(SpeechConstant.TTS_AUDIO_PATH,"./sdcard/iflutek.pcm");
        //Synthesis
        String content=editText_content.getText().toString();
        mTts.startSpeaking(content,mSynListener);
    }

    //Synthetic monitor
    private SynthesizerListener mSynListener=new SynthesizerListener() {
        @Override
        public void onSpeakBegin() {

        }

        @Override
        public void onBufferProgress(int i, int i1, int i2, String s) {

        }

        @Override
        public void onSpeakPaused() {

        }

        @Override
        public void onSpeakResumed() {

        }

        @Override
        public void onSpeakProgress(int i, int i1, int i2) {

        }

        @Override
        public void onCompleted(SpeechError speechError) {

        }

        @Override
        public void onEvent(int i, int i1, int i2, Bundle bundle) {

        }
    };
}

 

Tags: Android JSON network Mobile

Posted on Wed, 06 Nov 2019 14:13:04 -0500 by Myrkul