喚醒功能,顧名思義,經過語音,喚醒服務,作咱們想作的事情。前端
地址:http://download.csdn.net/detail/q4878802/9023213android
地址:http://blog.csdn.net/q4878802/article/details/47762169json
咱們要使用的是訊飛的付費功能,選擇喚醒服務,點擊下載之後,會提示沒有購買。點擊「購買服務」
app
點擊購買一會看到付費狀況,有項目須要,就必須購買,咱們寫Demo,訊飛給提供了體驗版的SDK,和正式版的沒有功能上的區別,可是隻能試用35天,裝機量只有3個,喚醒詞不能改,只有「訊飛語音」和「訊飛語點」兩個喚醒詞。
ide
assets目錄下是一些圖片資源文件工具
doc目錄下是一些開發文檔佈局
libs目錄下是一些jar包和so庫測試
res目錄下是語音的資源文件,很是重要this
sample目錄下是Demospa
能夠按照開發文檔的介紹,一步一步跟着作,實現喚醒功能
地址:http://www.xfyun.cn/doccenter/awd
我我的的習慣是看Demo,開發文檔只是作一個參考,我我的感受這樣更直接,開發效率更高。
這裏用到的喚醒功能不是全部的權限都用到的,具體用到了哪些權限,能夠看上面的連接,用到哪寫權限就加哪些權限,這個爲了快速方便測試,把訊飛用到的權限都加上了。
<uses-permission android:name="android.permission.RECORD_AUDIO" /> <uses-permission android:name="android.permission.INTERNET" /> <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.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.READ_CONTACTS" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
我是將appid的初始化放在的Applicaiton下,具體能夠下載源碼
// 應用程序入口處調用,避免手機內存太小,殺死後臺進程後經過歷史intent進入Activity形成SpeechUtility對象爲null // 如在Application中調用初始化,須要在Mainifest中註冊該Applicaiton // 注意:此接口在非主進程調用會返回null對象,如需在非主進程使用語音功能,請增長參數:SpeechConstant.FORCE_LOGIN+"=true" // 參數間使用「,」分隔。 // 設置你申請的應用appid StringBuffer param = new StringBuffer(); param.append("appid=55d33f09"); param.append(","); param.append(SpeechConstant.ENGINE_MODE + "=" + SpeechConstant.MODE_MSC); // param.append(","); // param.append(SpeechConstant.FORCE_LOGIN + "=true"); SpeechUtility.createUtility(InitKqwSpeech.this, param.toString());
初始化好了之後直接複製工具類就能夠用了
package com.example.kqwspeechdemo.engine; import org.json.JSONException; import org.json.JSONObject; import com.iflytek.cloud.SpeechConstant; import com.iflytek.cloud.SpeechError; import com.iflytek.cloud.SpeechUtility; 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.content.Context; import android.os.Bundle; import android.util.Log; import android.widget.Toast; /** * 語音喚醒 * * @author kongqw * */ public abstract class KqwWake { /** * 喚醒的回調 */ public abstract void kqwWake(); // Log標籤 private static final String TAG = "KqwWake"; // 上下文 private Context mContext; // 語音喚醒對象 private VoiceWakeuper mIvw; /* * TODO 設置門限值 : 門限值越低越容易被喚醒,須要本身反覆測試,根據不一樣的使用場景,設置一個比較合適的喚醒門限 */ // private final static int MAX = 60; // private final static int MIN = -20; private int curThresh = 40; public KqwWake(Context context) { mContext = context; // 加載識喚醒地資源,resPath爲本地識別資源路徑 StringBuffer param = new StringBuffer(); String resPath = ResourceUtil.generateResourcePath(context, RESOURCE_TYPE.assets, "ivw/55d33f09.jet"); param.append(ResourceUtil.IVW_RES_PATH + "=" + resPath); param.append("," + ResourceUtil.ENGINE_START + "=" + SpeechConstant.ENG_IVW); boolean ret = SpeechUtility.getUtility().setParameter(ResourceUtil.ENGINE_START, param.toString()); if (!ret) { Log.d(TAG, "啓動本地引擎失敗!"); } // 初始化喚醒對象 mIvw = VoiceWakeuper.createWakeuper(context, null); }; /** * 喚醒 */ public void wake() { // 非空判斷,防止因空指針使程序崩潰 mIvw = VoiceWakeuper.getWakeuper(); if (mIvw != null) { // textView.setText(resultString); // 清空參數 mIvw.setParameter(SpeechConstant.PARAMS, null); // 喚醒門限值,根據資源攜帶的喚醒詞個數按照「id:門限;id:門限」的格式傳入 mIvw.setParameter(SpeechConstant.IVW_THRESHOLD, "0:" + curThresh); // 設置喚醒模式 mIvw.setParameter(SpeechConstant.IVW_SST, "wakeup"); // 設置持續進行喚醒 mIvw.setParameter(SpeechConstant.KEEP_ALIVE, "1"); mIvw.startListening(mWakeuperListener); } else { Toast.makeText(mContext, "喚醒未初始化", Toast.LENGTH_SHORT).show(); } } public void stopWake() { mIvw = VoiceWakeuper.getWakeuper(); if (mIvw != null) { mIvw.stopListening(); } else { Toast.makeText(mContext, "喚醒未初始化", Toast.LENGTH_SHORT).show(); } } String resultString = ""; private WakeuperListener mWakeuperListener = new WakeuperListener() { @Override public void onResult(WakeuperResult result) { try { String text = result.getResultString(); JSONObject object; object = new JSONObject(text); 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")); resultString = buffer.toString(); stopWake(); kqwWake(); } catch (JSONException e) { resultString = "結果解析出錯"; e.printStackTrace(); } } @Override public void onError(SpeechError error) { Log.i(TAG, error.getPlainDescription(true)); } @Override public void onBeginOfSpeech() { Log.i(TAG, "開始說話"); } @Override public void onEvent(int eventType, int isLast, int arg2, Bundle obj) { } }; } 測試類 package com.example.kqwspeechdemo; import com.example.kqwspeechdemo.engine.KqwWake; import android.app.Activity; import android.os.Bundle; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity { private TextView mTvLog; private TextView mTvResult; private KqwWake kqwWake; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mTvLog = (TextView) findViewById(R.id.tv_log); mTvResult = (TextView) findViewById(R.id.tv_result); kqwWake = new KqwWake(this) { @Override public void kqwWake() { Toast.makeText(MainActivity.this, "Debug:\n喚醒成功", Toast.LENGTH_SHORT).show(); // 開啓喚醒 kqwWake.wake(); } }; // 開啓喚醒 kqwWake.wake(); } }
<LinearLayout 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:orientation="vertical" tools:context="com.example.kqwspeechdemo.MainActivity" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#FF000000" android:gravity="center" android:padding="10dp" android:text="喚醒詞:訊飛語音、訊飛語點" android:textColor="#FFFFFFFF" android:textSize="20dp" /> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="#FFFFFFFF" /> <TextView android:id="@+id/tv_log" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#FF000000" android:gravity="center" android:padding="10dp" android:text="錄音信息" android:textColor="#FFFFFFFF" android:textSize="10dp" /> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="#FFFFFFFF" /> <TextView android:id="@+id/tv_result" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#FF000000" android:padding="10dp" android:text="返回結果" android:textColor="#FFFFFFFF" android:textSize="10dp" /> </LinearLayout>