第一步:
http://www.xfyun.cn/在官网注册,创建android应用,申请APP_key,下载SDK文件
具体开发指南官方的开发文档:http://www.xfyun.cn/doccenter/awd
第二步:
导入SDK中的资料到对应的文件中如图
第三步:
1.添加权限:
<!--连接网络权限,用于执行云端语音能力 -->
<uses-permission android:name="android.permission.INTERNET"/>
<!--获取手机录音机使用权限,听写、识别、语义理解需要用到此权限 -->
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<!--读取网络信息状态 -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<!--获取当前wifi状态 -->
<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"/>
2.添加Gson依赖,在app/build.gradle中
compile 'com.google.code.gson:gson:2.8.0'
3,代码功能实现:
package com.loaderman.voicexunfeiyuyindemo;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import com.google.gson.Gson;
import com.iflytek.cloud.InitListener;
import com.iflytek.cloud.RecognizerResult;
import com.iflytek.cloud.SpeechConstant;
import com.iflytek.cloud.SpeechError;
import com.iflytek.cloud.SpeechUtility;
import com.iflytek.cloud.SynthesizerListener;
import com.iflytek.cloud.ui.RecognizerDialog;
import com.iflytek.cloud.ui.RecognizerDialogListener;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private TextView tv;
String str = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化,记得替换自己的App_key
SpeechUtility.createUtility(this, SpeechConstant.APPID + "=58bebbee");
tv = (TextView) findViewById(R.id.tv);
SynthesizerListener mSynListener = new SynthesizerListener() {
//会话结束回调接口,没有错误时, error为null
public void onCompleted(SpeechError error) {
}
//缓冲进度回调
//percent为缓冲进度0~100, beginPos为缓冲音频在文本中开始位置, endPos表示缓冲音频在
public void onBufferProgress(int percent, int beginPos, int endPos, String info) {
}
//开始播放
public void onSpeakBegin() {
}
//暂停播放
public void onSpeakPaused() {
}
//播放进度回调
//percent为播放进度0~100,beginPos为播放音频在文本中开始位置, endPos表示播放音频在文
public void onSpeakProgress(int percent, int beginPos, int endPos) {
}
//恢复播放回调接口
public void onSpeakResumed() {
}
//会话事件回调接口
public void onEvent(int arg0, int arg1, int arg2, Bundle arg3) {
}
};
findViewById(R.id.btn_speak).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//1.创建SpeechRecognizer对象,第二个参数:本地听写时传InitListener
RecognizerDialog iatDialog = new RecognizerDialog(MainActivity.this, new InitListener() {
@Override
public void onInit(int i) {
}
});
//2.设置听写参数
iatDialog.setParameter(SpeechConstant.DOMAIN, "iat");
iatDialog.setParameter(SpeechConstant.LANGUAGE, "zh_cn");//语言为普通话
iatDialog.setParameter(SpeechConstant.ACCENT, "mandarin ");
//3.设置回调接口
iatDialog.setListener(new RecognizerDialogListener() {
@Override
public void onResult(RecognizerResult result, boolean isLast) {
String resultString = result.getResultString();
Gson gson = new Gson();
VoiceBean voiceBean = gson.fromJson(resultString, VoiceBean.class);
List<VoiceBean.WsBean> ws = voiceBean.ws;
for (int i = 0; i < ws.size(); i++) {
VoiceBean.WsBean wsBean = ws.get(i);
List<VoiceBean.WsBean.CwBean> cw = wsBean.cw;
for (int j = 0; j < cw.size(); j++) {
VoiceBean.WsBean.CwBean cwBean = cw.get(j);
String w = cwBean.w;
str += w;
}
}
if (isLast) {//判断是否说完
tv.setText(str);
}
}
@Override
public void onError(SpeechError speechError) {
System.out.println(speechError.toString());
}
});
//4.开始听写
iatDialog.show();
}
});
}
}
package com.loaderman.voicexunfeiyuyindemo;
import java.util.List;
public class VoiceBean {
/**
* bg : 0
* ed : 0
* ls : false
* sn : 1
* ws : [{"bg":0,"cw":[{"sc":0,"w":"床"}]},{"bg":0,"cw":[{"sc":0,"w":"前"}]},{"bg":0,"cw":[{"sc":0,"w":"明"}]},{"bg":0,"cw":[{"sc":0,"w":"月光"}]}]
*/
public int bg;
public int ed;
public boolean ls;
public int sn;
public List<WsBean> ws;
public static class WsBean {
/**
* bg : 0
* cw : [{"sc":0,"w":"床"}]
*/
public int bg;
public List<CwBean> cw;
public static class CwBean {
/**
* sc : 0.0
* w : 床
*/
public double sc;
public String w;
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
tools:context="com.loaderman.voicexunfeiyuyindemo.MainActivity">
<Button
android:id="@+id/btn_speak"
android:layout_width="wrap_content"
android:text="点我说话"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="用来显示说话的文字"/>
</LinearLayout>
用真机测试,点击Button说话即可!