针对机器人 智能手表等的一项付费技术 官方提供35天三台装机量的试用,有兴趣的可以试着写写
首先是官方注册 资源下载 权限设置等等就不一一设置了 上主要部分的代码
main.java文件代码
package com.huanxing.lshuanxing;
import org.json.JSONException;
import org.json.JSONObject;
import com.iflytek.cloud.RecognizerResult;
import com.iflytek.cloud.SpeechConstant;
import com.iflytek.cloud.SpeechError;
import com.iflytek.cloud.SpeechEvent;
import com.iflytek.cloud.SpeechSynthesizer;
import com.iflytek.cloud.SpeechUtility;
import com.iflytek.cloud.SynthesizerListener;
import com.iflytek.cloud.VoiceWakeuper;
import com.iflytek.cloud.WakeuperListener;
import com.iflytek.cloud.WakeuperResult;
import com.iflytek.cloud.util.ResourceUtil;
import com.iflytek.cloud.util.ResourceUtil.RESOURCE_TYPE;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.app.Activity;
import android.view.Menu;
import android.widget.Toast;
public class MainActivity extends Activity {
private int curThresh = 40;
private VoiceWakeuper mivWa;
private SpeechSynthesizer mTts;
//定义的一个唤醒响应的数组
String[] ssreply={
"干啥!","咋了!","在呢!","你说话呀!"
};
//private KqwWake kqwWake;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//初始化及创建语音配置对象
SpeechUtility.createUtility(MainActivity.this,SpeechConstant.APPID+"=572be963");
setContentView(R.layout.activity_main);
initfirst();
}
private void initfirst(){
mTts=SpeechSynthesizer.createSynthesizer(MainActivity.this, null);
//语音唤醒代码集成
mivWa=VoiceWakeuper.createWakeuper(MainActivity.this, null);
//获取唤醒词路径
String respath=ResourceUtil.generateResourcePath(MainActivity.this, RESOURCE_TYPE.assets, "ivw/572be963.jet");
mivWa.setParameter(SpeechConstant.IVW_RES_PATH, respath);
mivWa.setParameter(SpeechConstant.IVW_THRESHOLD, "0:"+curThresh);
// 设置唤醒模式
mivWa.setParameter(SpeechConstant.IVW_SST, "wakeup");
// 设置持续进行唤醒
mivWa.setParameter(SpeechConstant.KEEP_ALIVE, "1");
mivWa.startListening(mWakeuperListener);
mivWa.setParameter(SpeechConstant.IVW_SST, "oneshot");
mivWa.setParameter(SpeechConstant.ENGINE_TYPE, "cloud");
mivWa.setParameter(SpeechConstant.IVW_NET_MODE, "2");
mivWa.startListening(mWakeuperListener);
}
private WakeuperListener mWakeuperListener=new WakeuperListener() {
public void onVolumeChanged(int arg0) {
// TODO Auto-generated method stub
}
public void onResult(WakeuperResult arg0) {
// TODO Auto-generated method stub
String text = arg0.getResultString();
JSONObject object = null;
try {
object = new JSONObject(text);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
StringBuffer buffer = new StringBuffer();
buffer.append("【RAW】 " + text);
buffer.append("\n");
buffer.append("【操作类型】" + object.optString("sst"));
buffer.append("\n");
buffer.append("【唤醒词id】" + object.optString("id"));
buffer.append("\n");
buffer.append("【得分】" + object.optString("score"));
buffer.append("\n");
buffer.append("【前端点】" + object.optString("bos"));
buffer.append("\n");
buffer.append("【尾端点】" + object.optString("eos"));
String esultString = buffer.toString();
Toast.makeText(MainActivity.this,"唤醒成功",1).show();
int a=(int)(Math.random() * 3+0);
String ssString=ssreply[a];
hecheng(ssString);
stopWake();
mivWa.startListening(mWakeuperListener);
}
public void onEvent(int arg0, int arg1, int arg2, Bundle arg3) {
// TODO Auto-generated method stub
if (SpeechEvent.EVENT_IVW_RESULT==arg0) {
RecognizerResult result=(RecognizerResult) arg3.get(SpeechEvent.KEY_EVENT_IVW_RESULT);
}
}
public void onError(SpeechError arg0) {
// TODO Auto-generated method stub
}
public void onBeginOfSpeech() {
// TODO Auto-generated method stub
}
};
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void stopWake() {
mivWa = VoiceWakeuper.getWakeuper();
if (mivWa != null) {
mivWa.stopListening();
} else {
Toast.makeText(MainActivity.this, "唤醒未初始化", Toast.LENGTH_SHORT).show();
}
}
private void setParam(){
// 设置发音人
mTts.setParameter(SpeechConstant.VOICE_NAME, "xiaoyan");
// 设置语速
mTts.setParameter(SpeechConstant.SPEED, "50");
// 设置音调
mTts.setParameter(SpeechConstant.PITCH, "50");
// 设置音量0-100
mTts.setParameter(SpeechConstant.VOLUME, "100");
// 设置播放器音频流类型
mTts.setParameter(SpeechConstant.STREAM_TYPE, "3");
}
private SynthesizerListener mLListener=new SynthesizerListener() {
public IBinder asBinder() {
// TODO Auto-generated method stub
return null;
}
public void onSpeakResumed() {
// TODO Auto-generated method stub
}
public void onSpeakProgress(int arg0, int arg1, int arg2) {
// TODO Auto-generated method stub
}
public void onSpeakPaused() {
// TODO Auto-generated method stub
}
public void onSpeakBegin() {
// TODO Auto-generated method stub
}
public void onEvent(int arg0, int arg1, int arg2, Bundle arg3) {
// TODO Auto-generated method stub
}
public void onCompleted(int arg0) throws RemoteException {
// TODO Auto-generated method stub
}
public void onBufferProgress(int arg0, int arg1, int arg2, String arg3) {
// TODO Auto-generated method stub
}
public void onCompleted(SpeechError arg0) {
// TODO Auto-generated method stub
}
};
//语音合成的方法实现
private void hecheng(String ss) {
// TODO Auto-generated method stub
setParam();
//
mTts.startSpeaking(ss,mLListener);
}
}
main.xml代码
<RelativeLayout 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=".MainActivity" >
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView2"
android:layout_marginTop="26dp"
android:text="尝试输入以下唤醒词:" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="69dp"
android:text="小明小明,叮叮当当,小牛吃草,校长哥哥,主席同志"
android:textSize="25dp" />
</RelativeLayout>
切记自己去注册定义唤醒词 还有appiD也换成自己的 如果有其他问题可以多多交流