Speech synthesis and recognition based on Android

Time:2021-1-13

Android speech synthesis and speech recognition, for your reference, the specific content is as follows

Here call iFLYTEK voice API, voice cloud open platform

To call iFLYTEK voice API, you need to add library files Msc.jar , add libmsc.so File, but also need to add permissions, specific steps can be seen in the SDK documentation

Refer to the development document and write a simple speech synthesis and recognition demo, as shown below

Input text in EditText and click speech synthesis to convert text into speech

Click speech synthesis and input speech. The recognized text will be displayed in the form of prompt and displayed in EditText

The main code is as follows. Note that you need to apply for appid yourself

package com.example.voice;
 
import java.util.ArrayList;
 
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
 
import com.iflytek.cloud.speech.*;
 
public class VoiceActivity extends Activity {
 private static final String APPID = "appid=52cddb99";
 private EditText et = null;
 private Button btn1 = null;
 private Button btn2 = null;
 String text = "";
 String temp="";
 
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_voice);
 et = (EditText) findViewById(R.id.et);
 btn1 = (Button) findViewById(R.id.btn1);
 btn1.setOnClickListener(mylistener);
 btn2 = (Button) findViewById(R.id.btn2);
 btn2.setOnClickListener(mylistener);
 }
 
 private OnClickListener mylistener = new OnClickListener() {
 public void onClick(View v) {
 SpeechUser.getUser().login(VoiceActivity.this, null, null, APPID,
 loginListener);
 Button btn = (Button) v;
 switch (btn.getId()) {
 case R.id.btn1:
 SpeechSynthesizer mSpeechSynthesizer = SpeechSynthesizer
 .createSynthesizer(VoiceActivity.this);
 mSpeechSynthesizer.setParameter(SpeechConstant.VOICE_NAME,
 "xiaoyu");
 mSpeechSynthesizer.setParameter(SpeechConstant.SPEED, "50");
 mSpeechSynthesizer.startSpeaking(et.getText().toString(),
 mSynListener);
 break;
 case R.id.btn2:
 text = "";
 temp="";
 SpeechRecognizer recognizer = SpeechRecognizer
 .createRecognizer(VoiceActivity.this);
 recognizer.setParameter(SpeechConstant.DOMAIN, "iat");
 recognizer.setParameter(SpeechConstant.LANGUAGE, "zh_cn");
 recognizer.setParameter(SpeechConstant.ACCENT, "accent");
 recognizer.startListening(mRecoListener);
 break;
 }
 }
 };
 private SynthesizerListener mSynListener = new SynthesizerListener() {
 public void onBufferProgress(int arg0, int arg1, int arg2, String arg3) {
 }
 
 public void onCompleted(SpeechError arg0) {
 }
 
 public void onSpeakBegin() {
 }
 
 public void onSpeakPaused() {
 }
 
 public void onSpeakProgress(int arg0, int arg1, int arg2) {
 }
 
 public void onSpeakResumed() {
 }
 };
 private RecognizerListener mRecoListener = new RecognizerListener() {
 public void onBeginOfSpeech() {
 }
 
 public void onEndOfSpeech() {
 }
 
 public void onError(SpeechError error) {
 
 }
 
 public void onEvent(int arg0, int arg1, int arg2, String arg3) {
 }
 
 public void onVolumeChanged(int arg0) {
 }
 
 public void onResult(RecognizerResult results, boolean isLast) {
 //Join the parsed strings together
 temp=results.getResultString();
 JsonParser json = new JsonParser();
 text+=json.parseIatResult(temp);
 if(isLast==true)
 {
 et.setText(text, null);
 Toast.makeText(VoiceActivity.this,text,Toast.LENGTH_LONG).show();
 }
 }
 };
 private SpeechListener loginListener = new SpeechListener() {
 public void onCompleted(SpeechError arg0) {
 }
 
 public void onData(byte[] arg0) {
 }
 
 public void onEvent(int arg0, Bundle arg1) {
 }
 };
 
 public boolean onCreateOptionsMenu(Menu menu) {
 // Inflate the menu; this adds items to the action bar if it is present.
 getMenuInflater().inflate(R.menu.voice, menu);
 return true;
 }
}

Layout file

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context=".VoiceActivity" >
 
 <TextView
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:gravity="center"
 android:text="VoiceApplication" />
 
 <EditText
 android:id="@+id/et"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:gravity="top"
 android:layout_weight="0.32" />
 
 <Button
 android:id="@+id/btn1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_weight="0.03"
 android:text= "Speech synthesis" / >
 
 <Button 
 android:id="@+id/btn2"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_weight="0.03"
 android:text= "Speech recognition"
 
</TableLayout>

The data in JSON format is analyzed with reference to iFLYTEK’s documents

package com.example.voice;
 
import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONTokener;
 
import android.text.TextUtils;
 
//import com.iflytek.speech.ErrorCode;
//import com.iflytek.speech.SpeechError;
/**
 *Analyze the JSON results returned from the cloud
 * 
 * @author iFlytek
 * @since 20131211
 */
public class JsonParser {
 
 /**
 *An analysis of dictation results in JSON format
 * 
 * @param json
 * @return
 */
 public static String parseIatResult(String json) {
 if (TextUtils.isEmpty(json))
 return "";
 
 StringBuffer ret = new StringBuffer();
 try {
 JSONTokener tokener = new JSONTokener(json);
 JSONObject joResult = new JSONObject(tokener);
 
 JSONArray words = joResult.getJSONArray("ws");
 for (int i = 0; i < words.length(); i++) {
 //Dictation result word, default to use the first result
 JSONArray items = words.getJSONObject(i).getJSONArray("cw");
 JSONObject obj = items.getJSONObject(0);
 ret.append(obj.getString("w"));
 //If multiple candidate results are needed, other fields in the array are parsed
 // for(int j = 0; j < items.length(); j++)
 // {
 // JSONObject obj = items.getJSONObject(j);
 // ret.append(obj.getString("w"));
 // }
 }
 } catch (Exception e) {
 e.printStackTrace();
 }
 return ret.toString();
 }

}

The above is the whole content of this article, I hope to help you learn, and I hope you can support developer more.