通過一番調研,發現國內上市公司科大訊飛的語音+SDK包口碑好,效果好,集成也方便。語音+支持離線應用,這是最主要的。集成過程走起:
1、下載DEMO程序。DEMO程序就包含了全部需要的資源。
2、SDK集成關鍵代碼:
(1)SpeechSynthesizer 是 jar包里封裝的語音合成對象她是我們的女主角。
// 語音合成對象private SpeechSynthesizer mTts;public static String SPEAKER = “speaker”;
從demo照抄實例化mTts的代碼。調用方式應該是jar里封裝好的方法外包調用,暫時不關注,先關注一下怎樣用起來。
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)檢查語音加引擎是否安裝
/** * 執行本地安裝 語音+ * * @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, “安裝語音引擎失敗!”, 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;}/** * 語音合成的參數設置 *
* @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”));}
(3)初期化監聽,我在ErrorCode.SUCCESS初始化成功后顯示語音引擎可用的狀態,否則紅叉顯示不可以狀態。合成回調監聽,這里為了避免異步方式調用,聽不清楚,使用了局部變量記錄當前的語音播報狀態isPlaySound。引擎支持異步調用,如果不控制會出現,一句話沒說完,其他調用又在發起請求的情況,使用了隊列來控制播報的順序。
/** * 初期化監聽。 */
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);}}};/** * 合成回調監聽。
*/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 {}};/** * 窗體銷毀時釋放語音服務資源 */
@Overrideprotected void onDestroy() {super.onDestroy();mTts.stopSpeaking(mTtsListener);mTts.destory();}
3、完成。截圖紀念一下:
評論
查看更多