网站建设资讯

NEWS

网站建设资讯

Android中怎么实现语音合成与识别功能

今天就跟大家聊聊有关Android中怎么实现语音合成与识别功能,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

创新互联建站2013年至今,先为沁阳等服务建站,沁阳等地企业,进行企业商务咨询服务。为沁阳企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。

具体内容如下

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) { //将解析后的字符串连在一起 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; }}

布局文件