通過一番調(diào)研,發(fā)現(xiàn)國內(nèi)上市公司科大訊飛的語音+SDK包口碑好,效果好,集成也方便。語音+支持離線應(yīng)用,這是最主要的。集成過程走起:
1、下載DEMO程序。DEMO程序就包含了全部需要的資源。
2、SDK集成關(guān)鍵代碼:
?。?)SpeechSynthesizer 是 jar包里封裝的語音合成對象她是我們的女主角。
// 語音合成對象private SpeechSynthesizer mTts;public staTIc String SPEAKER = “speaker”;
從demo照抄實(shí)例化mTts的代碼。調(diào)用方式應(yīng)該是jar里封裝好的方法外包調(diào)用,暫時不關(guān)注,先關(guān)注一下怎樣用起來。
private void checkVoiceServiceIsInstalled()
{// 檢測語音引擎是否可用,如果沒有安裝自動安裝if (!checkSpeechServiceInstall())
{String assetsApk = “SpeechService.apk”;
if (processInstall(this, assetsApk))SpeechUTIlity.getUTIlity(this).setAppid(“53c776aa”);return;}// 引擎初始化
SpeechUTIlity.getUtility(this).setAppid(“53c776aa”);
mTts = new SpeechSynthesizer(this, mTtsInitListener);}
(2)檢查語音加引擎是否安裝
/** * 執(zhí)行本地安裝 語音+ * * @param context * @param assetsApk * @return */private boolean processInstall(Context context, String assetsApk)
{try {Thread.sleep(30000);} catch (InterruptedException e) {e.printStackTrace();}// 本地安裝方式
if (!ApkInstaller.installFromAssets(context, assetsApk))
{Toast.makeText(activity_main.this, “安裝語音引擎失?。?rdquo;, Toast.LENGTH_SHORT).show();return false;}return true;}/** * 檢測科大訊飛語音+引擎是否安裝 *
* @return */private boolean checkSpeechServiceInstall() {String packageName = “com.iflytek.speechcloud”;
List
for (int i = 0; i 《 packages.size(); i++) {PackageInfo packageInfo = packages.get(i);
if (packageInfo.packageName.equals(packageName)) {return true;} else {continue;}}return false;}/** * 語音合成的參數(shù)設(shè)置 *
* @param param * @return */private void setParam() {mTts.setParameter(SpeechConstant.ENGINE_TYPE,sp.getString(“engine_preference”, “local”));
if (sp.getString(“engine_preference”, “local”).equalsIgnoreCase(“local”))
{mTts.setParameter(SpeechSynthesizer.VOICE_NAME,sp.getString(“role_cn_preference”, “xiaoyan”));}
else {mTts.setParameter(SpeechSynthesizer.VOICE_NAME,sp.getString(“role_cn_preference”, “xiaoyan”));}
mTts.setParameter(SpeechSynthesizer.SPEED,sp.getString(“speed_preference”, “50”));
mTts.setParameter(SpeechSynthesizer.PITCH,sp.getString(“pitch_preference”, “50”));
mTts.setParameter(SpeechSynthesizer.VOLUME,sp.getString(“volume_preference”, “50”));}
?。?)初期化監(jiān)聽,我在ErrorCode.SUCCESS初始化成功后顯示語音引擎可用的狀態(tài),否則紅叉顯示不可以狀態(tài)。合成回調(diào)監(jiān)聽,這里為了避免異步方式調(diào)用,聽不清楚,使用了局部變量記錄當(dāng)前的語音播報(bào)狀態(tài)isPlaySound。引擎支持異步調(diào)用,如果不控制會出現(xiàn),一句話沒說完,其他調(diào)用又在發(fā)起請求的情況,使用了隊(duì)列來控制播報(bào)的順序。
/** * 初期化監(jiān)聽。 */
private InitListener mTtsInitListener = new InitListener() {@Overridepublic void onInit(ISpeechModule arg0, int code)
{if (code == ErrorCode.SUCCESS) {btn_voiceDemo.setBackgroundResource(R.drawable.voice_enable_style);}
else {btn_voiceDemo.setBackgroundResource(R.drawable.voice_disable_style);}}};/** * 合成回調(diào)監(jiān)聽。
*/private SynthesizerListener mTtsListener = new SynthesizerListener.Stub()
{@Overridepublic void onBufferProgress(int progress) throws RemoteException {}@Overridepublic void onCompleted(int code)
throws RemoteException {activity_main.this.runOnUiThread(new Runnable() {@Overridepublic void run() {int pos = voiceQueueList.indexOf(getCurrentVoiceEntity());
if (pos != -1) {currentVoiceEntity.setVoicePlayNum(currentVoiceEntity.getVoicePlayNum() + 1);
voiceQueueList.remove(pos);
if (currentVoiceEntity.getVoicePlayNum() 》 Integer.parseInt(sp.getString(“voice_play_timers”, “0”))) {currentVoiceEntity.setIsVoicePlayed(true);
new ExecVoiceStatusChangeBiz().execute(String.valueOf(getCurrentVoiceEntity().getKeyId()));}
voiceQueueList.add(currentVoiceEntity);}loadVoiceTaskList();}});isPlaySound = false;}@Overridepublic void onSpeakBegin()
throws RemoteException {isPlaySound = true;}@Overridepublic void onSpeakPaused()
throws RemoteException {}@Overridepublic void onSpeakProgress(int progress)
throws RemoteException {}@Overridepublic void onSpeakResumed()
throws RemoteException {}};/** * 窗體銷毀時釋放語音服務(wù)資源 */
@Overrideprotected void onDestroy() {super.onDestroy();mTts.stopSpeaking(mTtsListener);mTts.destory();}
3、完成。截圖紀(jì)念一下: