这里是使用讯飞语言输入,具体步骤以下:
1、获得APPID
首先先到讯飞语音云官网注册1个账号http://www.voicecloud.cn/
2、然后在我的语音云中创建1个新利用,选择语音基础能力,就能够看到Appid:××××××××
(注意:每一个appid都是会有相对应的sdk包,不然无效)
3、创建1个新工程VoiceTest,并将下载的SDK包中的libs目录下的库导入到工程VoiceTest中的libs里面(直接复制过去便可)
4、打开sdk包中的doc目录下的msc文件,里面会有以上的几步步骤;
权限:
<!--连接网络权限,用于履行云端语音能力 -->
<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"/>
5、把sdk包中的sample(demo代码)导入Eclipse中,以便参考,其实就是要用到demo中的json解包方法
6、程序若在此过不去,也就是你的appid与相对应的sdk包有问题
//5、显示听写对话框 isrDialog.show();
7、简单语音测试代码
<span style="font-size:14px;"><strong> <span style="color:#FF0000;"> <MainAcctivity.java></span>
package com.example.voicetest;
import com.iflytek.cloud.InitListener;
import com.iflytek.cloud.RecognizerListener;
import com.iflytek.cloud.RecognizerResult;
import com.iflytek.cloud.SpeechConstant;
import com.iflytek.cloud.SpeechError;
import com.iflytek.cloud.SpeechRecognizer;
import com.iflytek.cloud.SpeechUtility;
import com.iflytek.cloud.ui.RecognizerDialog;
import com.iflytek.cloud.ui.RecognizerDialogListener;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
private EditText editText;
private Button speak;
private SpeechRecognizer miat;
private static final String APPID = "appid=5450a81b";
private RecognizerDialogListener recognizeListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText)findViewById(R.id.textview);
speak = (Button)findViewById(R.id.button1);
//设置按键监听
speak.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
InitListener InitListener = null;
//1、创建用户语音对象
SpeechUtility.createUtility(getBaseContext(), SpeechConstant.APPID +"=5450a81b");
RecognizerDialog isrDialog = new RecognizerDialog(MainActivity.this, InitListener);
//2、创建辨认对话框
isrDialog.setParameter(SpeechConstant.CLOUD_GRAMMAR, null);
//3、定义听写ui对话框,设置听写参数
isrDialog.setParameter(SpeechConstant.DOMAIN, "iat");
isrDialog.setParameter(SpeechConstant.SAMPLE_RATE, "8000");
//4、设置回调接口
isrDialog.setListener(recognizeListener);
//5、显示听写对话框
isrDialog.show();
//6、辨认成功时回调此方法
recognizeListener =new RecognizerDialogListener() {
@Override
public void onResult(RecognizerResult arg0, boolean arg1) {
// TODO Auto-generated method stub
//7、返回数据用json解包,并显示在控件上
String text = JsonParser.parseIatResult(arg0.getResultString());
editText.append(text);
editText.setSelection(editText.length());
}
@Override
public void onError(SpeechError arg0) {
// 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;
}
}
<span style="color:#FF0000;"> <JsonParser.java></span>
package com.example.voicetest;
import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONTokener;
/**
* Json结果解析类
*/
public class JsonParser {
public static String parseIatResult(String json) {
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++) {
// 转写结果词,默许使用第1个结果
JSONArray items = words.getJSONObject(i).getJSONArray("cw");
JSONObject obj = items.getJSONObject(0);
ret.append(obj.getString("w"));
}
} catch (Exception e) {
e.printStackTrace();
}
return ret.toString();
}
public static String parseGrammarResult(String json) {
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");
for(int j = 0; j < items.length(); j++)
{
JSONObject obj = items.getJSONObject(j);
if(obj.getString("w").contains("nomatch"))
{
ret.append("没有匹配结果");
return ret.toString();
}
ret.append("【结果】" + obj.getString("w"));
ret.append("【置信度】" + obj.getInt("sc"));
ret.append("
");
}
}
} catch (Exception e) {
e.printStackTrace();
ret.append("没有匹配结果.");
}
return ret.toString();
}
}
(activity_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" >
<AutoCompleteTextView
android:id="@+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/button1"
android:layout_centerHorizontal="true"
android:layout_marginBottom="30dp"
android:text="AutoCompleteTextView" >
<requestFocus />
</AutoCompleteTextView>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textview"
android:layout_alignParentBottom="true"
android:layout_marginBottom="164dp"
android:layout_marginLeft="22dp"
android:text="Button" />
</RelativeLayout></strong></span>