Android TTS speech synthesis experience sharing

Through some research, it was found that the voice +SDK package of the domestic listed company Keda Xunfei is good, the effect is good, and the integration is convenient. Voice + support for offline applications, this is the main thing. The integration process walks:

1. Download the DEMO program. The DEMO program contains all the resources needed.

Xunfei tts Android TTS voice synthesis experience sharing (科大讯飞语音+) integration

2, SDK integration key code:

(1) SpeechSynthesizer is a speech synthesis object encapsulated in a jar package. She is our heroine.

// speech synthesis object private SpeechSynthesizer mTts; public staTIc String SPEAKER = "speaker";

Instantiate the code for instantiating mTts from the demo. The calling method should be the outsourced call of the method encapsulated in the jar. For the time being, don't pay attention to it. First, pay attention to how to use it.

Private void checkVoiceServiceIsInstalled()

{// Check if the speech engine is available, if not installed automatically install if (!checkSpeechServiceInstall())

{String assetsApk = "SpeechService.apk";

If (processInstall(this, assetsApk))SpeechUTIlity.getUTIlity(this).setAppid("53c776aa");return;}// Engine initialization

SpeechUTIlity.getUtility(this).setAppid("53c776aa");

mTts = new SpeechSynthesizer(this, mTtsInitListener);}

(2) Check if the voice plus engine is installed

/** * Perform local installation of voice + * * @param context * @param assetsApk * @return */private boolean processInstall(Context context, String assetsApk)

{try {Thread.sleep(30000);} catch (InterruptedException e) {e.printStackTrace();}// Local installation method

If (!ApkInstaller.installFromAssets(context, assetsApk))

{Toast.makeText(activity_main.this, "Failed to install speech engine!", Toast.LENGTH_SHORT).show();return false;}return true;}/** * Check if the major voice + engine is installed*

* @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;}/** * Parameter settings for speech synthesis*

* @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) Initialization monitoring, I display the status of the speech engine after the initial initialization of ErrorCode.SUCCESS, otherwise the red cross shows the status. Synthetic callback listener, here to avoid asynchronous calls, can not hear clearly, using local variables to record the current voice broadcast state isPlaySound. The engine supports asynchronous calls. If no control occurs, one sentence is not finished, and other calls are in the case of initiating a request, and a queue is used to control the order of the broadcast.

/** * Initialization monitoring. */

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);}}};/** * Synthetic callback listener.

*/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 {}};/** * Release voice service resources when the form is destroyed*/

@Overrideprotected void onDestroy() {super.onDestroy();mTts.stopSpeaking(mTtsListener);mTts.destory();}

3. Complete. Take a screenshot to commemorate:

Xunfei tts Android TTS voice synthesis experience sharing (科大讯飞语音+) integration

Xunfei tts Android TTS voice synthesis experience sharing (科大讯飞语音+) integration

Generator TR&WR GFCI UL

Product features:
â– Good heat dissipation
--Blade design of copper alloy contacts,good conductivity
â– Super high impact resistance and thermal stability
--Cover pans are made of high quality polycarbonate
â– Chemical corrosion resistance
-- Fingerprint-resistant zinc plated mounting brackets
â– Grounding
--One-piece grounding design,No Load-Weather resistance

Generator TR&WR GFCI UL,Electrical Plugs UL,GFCI Sockets for Generator,Self-test Generator TR&WR GFCI UL

Hoojet Electric Appliance Co.,Ltd , https://www.hoojetgfci.com