Android基于訊飛語音SDK實(shí)現(xiàn)語音識別
一、準(zhǔn)備工作
1、你需要android手機(jī)應(yīng)用開發(fā)基礎(chǔ)
2、科大訊飛語音識別SDK android版
3、科大訊飛語音識別開發(fā)API文檔
4、android手機(jī)
關(guān)于科大訊飛SDK及API文檔,請到科大語音官網(wǎng)下載:http://www.xfyun.cn/
當(dāng)然SDK和API有多個(gè)版本可選,按照你的需要下載,其次,下載需要填寫資料申請注冊,申請通過或可獲得Appid
如下圖,申請一個(gè)APPID,就可以了。

二、語音識別流程
1、創(chuàng)建識別控件
函數(shù)原型
Public RecognizerDialog(Context context,String params)
其中Context表示當(dāng)前上下文環(huán)境,傳this即可
Params有參數(shù)詳見API文檔
2、用Appid登錄到科大訊飛服務(wù)器(自動連接,需要聯(lián)網(wǎng))
主要用到SpeechUser(com.iflytek.speech包下)類下的getUser().login()函數(shù)
其中g(shù)etUser()表示獲取用戶對象,可以實(shí)現(xiàn)用戶登錄,注銷等操作
Login函數(shù)原型
Public boolean login(Context context,String usr,String pwd,String
參數(shù)詳見API文檔
3、讀取語言識別語法
詳見API文檔
4、設(shè)置識別參數(shù)及識別監(jiān)聽器
通過RecognizerDialog下的setEngine()方法設(shè)置參數(shù)
函數(shù)原型
public void setEngine(String engine,String params,String grammar)
詳細(xì)的參數(shù)請參考API文檔
5、識別結(jié)果回調(diào)
需要實(shí)現(xiàn)RecognizerDialogListener接口,其中有兩個(gè)方法需要重寫,分別是
1)public void onResults(ArrayList<RecognizerResult> results,boolean isLast)
其中result是RecognizerResult對象的集合,RecognizerResult的屬性有
String text 識別文本
Int confidence 識別可信度
2)public void onEnd(SpeechError error)
6、識別結(jié)果處理(自行處理)
自己將文本進(jìn)行處理。
三、詳細(xì)開發(fā)過程
1、新建Android項(xiàng)目
和普通的android項(xiàng)目一樣,只是需要加入科大訊飛語言SDK包,主要包括
Msc.jar及l(fā)ibmsc.so動態(tài)庫文件,項(xiàng)目lib截圖

2、布局
這里只進(jìn)行簡單的布局,只設(shè)置一個(gè)按鈕作為語言識別按鈕及一個(gè)文本組件用作顯示識別結(jié)果,布局文件如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:context="${relativePackage}.${activityClass}" >
<EditText
android:id="@+id/editText"
android:layout_width="fill_parent"
android:layout_height="300dp"
android:gravity="top"
android:inputType="textMultiLine" >
<requestFocus />
</EditText>
<Button
android:id="@+id/button_start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="點(diǎn)擊開始說話" />
</LinearLayout>
3、Main.java代碼如下:
public class Main extends Activity {
// 組件
private Button button = null;
private TextView result = null;
private Toast mToast = null;
// 語音識別
private final String APP_ID = "這里改成自己的APP_ID";
private RecognizerDialog recognizerDialog = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.main);
button = (Button) super.findViewById(R.id.button_start);
result = (TextView) super.findViewById(R.id.editText);
// 初始化識別
mToast = Toast.makeText(this, "", Toast.LENGTH_SHORT);
mToast.setMargin(0f, 0.2f);
recognizerDialog = new RecognizerDialog(this, "appid=" + APP_ID);
SpeechUser.getUser().login(this, null, null, "appid=" + APP_ID, loginListener);
this.button.setOnClickListener(new Btn());
}
private class Btn implements OnClickListener {
@Override
public void onClick(View v) {
//MainActivity.this.voice.setImageResource(R.drawable.voicelight);
recognizerDialog.setListener(mRecoListener);
recognizerDialog.setEngine(null, "grammar_type=abnf", grammarText);
recognizerDialog.show();
}
}
// 語音識別用戶登錄監(jiān)聽器
private SpeechListener loginListener = new SpeechListener() {
@Override
public void onData(byte[] arg0) {
}
@Override
public void onEvent(int arg0, Bundle arg1) {
}
@Override
public void onEnd(SpeechError arg0) {
// TODO Auto-generated method stub
if (arg0 != null) {
mToast.setText("登錄失敗");
mToast.show();
} else {
mToast.setText("登錄成功");
mToast.show();
}
}
};
// 識別結(jié)果回調(diào)
private RecognizerDialogListener mRecoListener = new RecognizerDialogListener() {
@Override
public void onEnd(SpeechError error) {
//MainActivity.this.voice.setImageResource(R.drawable.voice);
}
@Override
public void onResults(ArrayList<RecognizerResult> results, boolean isLast) {
// TODO Auto-generated method stub
String text = "";
text = results.get(0).text;
mToast.setText("識別結(jié)果為:" + text);
mToast.show();
result.setText("識別結(jié)果為:" + text);
}
};
}
4、需要的權(quán)限:
<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"/>
5、結(jié)果截圖:


以上就是本文的全部內(nèi)容了,希望小伙伴們能夠喜歡。
相關(guān)文章
Android Service服務(wù)詳細(xì)介紹及使用總結(jié)
這篇文章主要介紹了Android Service 服務(wù)的詳細(xì)資料,網(wǎng)上關(guān)于Android Service 服務(wù)的文章比較多,但是不是很全面,不夠細(xì)致,畢竟是Android 四大組件之一,重要性不用說,這里總結(jié)下,需要的朋友可以參考下2016-12-12
Android實(shí)現(xiàn)的可以調(diào)整透明度的圖片查看器實(shí)例
這篇文章主要介紹了Android實(shí)現(xiàn)的可以調(diào)整透明度的圖片查看器,需要的朋友可以參考下2014-07-07
Flutter將整個(gè)App變?yōu)榛疑暮唵螌?shí)現(xiàn)方法
Flutter?是?Google?開源的?UI?工具包,幫助開發(fā)者通過一套代碼庫高效構(gòu)建多平臺精美應(yīng)用,這篇文章主要給大家介紹了關(guān)于Flutter將整個(gè)App變?yōu)榛疑膶?shí)現(xiàn)方法,在Flutter中實(shí)現(xiàn)整個(gè)App變?yōu)榛疑欠浅:唵蔚?需要的朋友可以參考下2021-12-12
Android自定義Dialog實(shí)現(xiàn)文字動態(tài)加載效果
這篇文章主要為大家詳細(xì)介紹了Android自定義Dialog實(shí)現(xiàn)文字動態(tài)加載效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-08-08
Android中使用GridView和ImageViewSwitcher實(shí)現(xiàn)電子相冊簡單功能實(shí)例
本篇文章主要介紹了Android中使用GridView和ImageViewSwitcher實(shí)現(xiàn)電子相冊簡單功能實(shí)例,具有一定的參考價(jià)值,有需要的可以了解一下。2016-12-12
Android 開發(fā)程序鎖應(yīng)用簡單實(shí)例
這篇文章主要介紹了Android 開發(fā)程序鎖應(yīng)用簡單實(shí)例的相關(guān)資料,需要的朋友可以參考下2016-10-10
Android中okhttp3.4.1+retrofit2.1.0實(shí)現(xiàn)離線緩存
這篇文章主要介紹了Android中okhttp3.4.1結(jié)合retrofit2.1.0實(shí)現(xiàn)離線緩存,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10
Android解決getExternalStorageDirectory在29后廢棄問題(推薦)
這篇文章主要介紹了Android解決getExternalStorageDirectory在29后廢棄問題(推薦),本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-02-02

