Android实现语音合成与识别功能
作者:suancaiyucoding 时间:2023-10-01 01:41:00
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;
}
}
布局文件
<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="语音合成" />
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.03"
android:text="语音识别" />
</TableLayout>
解析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;
/**
* 对云端返回的Json结果进行解析
*
* @author iFlytek
* @since 20131211
*/
public class JsonParser {
/**
* 听写结果的Json格式解析
*
* @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++) {
// 听写结果词,默认使用第一个结果
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();
}
}
来源:https://blog.csdn.net/suancaiyucoding/article/details/19121799
标签:Android,语音合成,语音识别
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
c语言实现可自定义的游戏地图
2021-10-25 14:38:34
![](https://img.aspxhome.com/file/2023/8/112298_0s.jpg)
浅谈Ribbon、Feign和OpenFeign的区别
2022-12-21 15:40:27
![](https://img.aspxhome.com/file/2023/5/86055_0s.png)
Java毕业设计实战之宠物医院与商城一体的系统的实现
2023-06-02 13:28:44
![](https://img.aspxhome.com/file/2023/8/72518_0s.jpg)
C# Bitmap 复制的小例子
2023-01-19 12:49:39
javascript最新2020经典面试题
2023-05-15 09:04:43
word ppt excel文档转换成pdf的C#实现代码
2022-10-10 16:51:54
c#和java base64不一致的解决方法
2022-11-24 02:18:55
![](https://img.aspxhome.com/file/2023/0/68740_0s.png)
AQS加锁机制Synchronized相似点详解
2023-08-04 22:36:55
![](https://img.aspxhome.com/file/2023/8/57918_0s.png)
解析Java中如何获取Spring中配置的bean
2023-07-20 13:35:26
Java中一些基础概念的使用详解
2023-07-30 23:35:23
springboot更新配置Swagger3的一些小技巧
2023-08-28 06:31:43
一篇文章带你了解java接口与继承
2022-07-15 02:44:08
Java 给PDF签名时添加可信时间戳的方法
2022-04-03 12:38:48
![](https://img.aspxhome.com/file/2023/9/96129_0s.png)
Android利用AsyncTask异步类实现网页内容放大缩小
2022-11-28 05:34:47
Spring配置shiro时自定义Realm中属性无法使用注解注入的解决办法
2022-09-18 11:18:22
![](https://img.aspxhome.com/file/2023/2/83122_0s.jpg)
Java中的main函数的详细介绍
2021-10-03 16:09:16
Java 数组高频考点分析讲解
2021-09-01 13:14:36
![](https://img.aspxhome.com/file/2023/2/61152_0s.png)
Sprint Boot 集成MongoDB的操作方法
2022-11-13 07:10:37
C#环形缓冲区(队列)完全实现
2022-06-26 08:05:48
IntelliJ IDEA 创建spring boot 的Hello World 项目(图解)
2023-03-10 16:13:45
![](https://img.aspxhome.com/file/2023/3/102133_0s.jpg)