基于android的语音识别

时间:2022-12-30 20:31:45

1.注册账户,添加应用

基于android的语音识别

 

2.针对android平台的选择应用,下载SDK

基于android的语音识别

 

3.将SDK的libs下文件拷贝到工程的libs目录下

基于android的语音识别

4.添加用户权限

    <uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
<uses-permission android:name="android.permission.CAMERA"/>

5.初始化语音配置对象

        //初始化语音配置对象,必须,这样才能使用MSC语音服务
SpeechUtility.createUtility(this, SpeechConstant.APPID+"=59454d78");

6.拷贝assets目录下资源文件到android工程的assets目录

基于android的语音识别

 

7.识别代码

       //创建语音识别对话框
RecognizerDialog rd = new RecognizerDialog(this,null);
//设置参数accent,language等参数
rd.setParameter(SpeechConstant.LANGUAGE,"zh_cn");//中文
rd.setParameter(SpeechConstant.ACCENT,"mandarin");//普通话
//设置回调接口
rd.setListener(new RecognizerDialogListener() {
@Override
public void onResult(RecognizerResult recognizerResult, boolean b) {
//获取返回结果
String result = recognizerResult.getResultString();
Log.e(
"result",result);
Log.e(
"b",b+"");
}

@Override
public void onError(SpeechError speechError) {

}
});
//显示对话框
rd.show();

8.由于返回的是json字符串所以还得解析出来才能使用

result:{"sn":2,"ls":true,"bg":0,"ed":0,"ws":[{"bg":0,"cw":[{"sc":0.00,"w":"?"}]}]}

9.解析Json字符串

        /**
* 解析json串
*
@param result
*/
protected String parseJsonData(String json) {
// 使用Gson来解析Json串
Gson gson = new Gson();
VoiceBean bean
= gson.fromJson(json, VoiceBean.class);
ArrayList
<WS> ws = bean.ws;
StringBuffer sb
= new StringBuffer();
for (WS ws2 : ws) {
String info
= ws2.cw.get(0).w;
sb.append(info);
}
return sb.toString();
}


10.完整代码

package com.example.chat;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.example.chat.VoiceBean.WS;
import com.google.gson.Gson;
import com.iflytek.cloud.RecognizerResult;
import com.iflytek.cloud.SpeechConstant;
import com.iflytek.cloud.SpeechError;
import com.iflytek.cloud.SpeechSynthesizer;
import com.iflytek.cloud.SpeechUtility;
import com.iflytek.cloud.ui.RecognizerDialog;
import com.iflytek.cloud.ui.RecognizerDialogListener;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;

public class MainActivity extends Activity {

StringBuffer strbuf;
ListView mlv;
private List<TalkBean> lists;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化语音配置对象,必须,这样才能使用MSC语音服务
SpeechUtility.createUtility(this, SpeechConstant.APPID+"=59454d78");
mlv
= (ListView) findViewById(R.id.lv);
lists
= new ArrayList<TalkBean>();

}

public void click(View view){
//创建语音识别对话框
RecognizerDialog rd = new RecognizerDialog(this,null);
//设置参数accent,language等参数
rd.setParameter(SpeechConstant.LANGUAGE,"zh_cn");//中文
rd.setParameter(SpeechConstant.ACCENT,"mandarin");//普通话
strbuf = new StringBuffer();
//设置回调接口
rd.setListener(new RecognizerDialogListener() {

@Override
public void onResult(RecognizerResult arg0, boolean arg1) {
// TODO Auto-generated method stub
String result = arg0.getResultString();
String data
= parseJsonData(result);
Log.e(
"data",data);

strbuf.append(data);
if(arg1){//回话结束
String voice = strbuf.toString();
Log.e(
"result",voice);
}

Log.e(
"b",arg1+"");
}

@Override
public void onError(SpeechError arg0) {
// TODO Auto-generated method stub

}
});
//显示对话框
rd.show();
}
/**
* 解析json串
*
@param result
*/
protected String parseJsonData(String json) {
// 使用Gson来解析Json串
Gson gson = new Gson();
VoiceBean bean
= gson.fromJson(json, VoiceBean.class);
ArrayList
<WS> ws = bean.ws;
StringBuffer sb
= new StringBuffer();
for (WS ws2 : ws) {
String info
= ws2.cw.get(0).w;
sb.append(info);
}
return sb.toString();
}

 

 

11.运行结果展示

基于android的语音识别