栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 移动开发 > Android

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

Android 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

Android语音合成与语音识别,供大家参考,具体内容如下

这里调用科大讯飞语音的API,语音云开放平台介绍

调用科大讯飞语音的API,需要加添库文件Msc.jar,添加libmsc.so文件,还需添加权限,具体步骤可参看SDK里的文档

参看开发的文档写了一个简单的语音合成和识别demo,图示如下

在EditText里输入文字,点击语音合成,可以实现文字转化为语音

点击语音合成,输入语音,识别的文字以提示的形式显示,并且显示在EditText中

主要代码如下,注意appid需要自己申请

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;
 }
}

布局文件


 
 
 
 
 
 

解析Json格式的数据是参照讯飞的文档中的

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;

public class JsonParser {
 
 
 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++) {
 // 听写结果词,默认使用第一个结果
 JSonArray items = words.getJSonObject(i).getJSonArray("cw");
 JSonObject obj = items.getJSonObject(0);
 ret.append(obj.getString("w"));
 // 如果需要多候选结果,解析数组其他字段
 // 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();
 }

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/154893.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号