Note: offline speech synthesis and online speech synthesis can be realized.
The mainactivity. Java code is as follows:
Package COM. chen. test16; import android. app. activity; import android. OS. bundle; import android. OS. remoteException; import android. view. view; import android. view. view. onclicklistener; import android. widget. button; import android. widget. textview; import COM. iflytek. speech. ispeechmodule; import COM. iflytek. speech. initlistener; import COM. iflytek. speech. speechconstant; import COM. iflytek. speech. speechsynthesizer; import COM. iflytek. speech. speechutility; import COM. iflytek. speech. synthesizerlistener; public class mainactivity extends activity {private textview; private button; speechsynthesizer MTTS; @ overrideprotected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. activity_main); textview = (textview) findviewbyid (R. id. textview); button = (button) findviewbyid (R. id. button); button. setonclicklistener (New buttonlistener (); If (speechutility. getutility (this ). queryavailableengines () = NULL | speechutility. getutility (this ). queryavailableengines (). length <= 0) {textview. settext ("Download");} speechutility. getutility (this ). setappid ("51b6eedb"); MTTS = new speechsynthesizer (this, mttsinitlistener);} class buttonlistener implements onclicklistener {@ overridepublic void onclick (view V) {// todo auto-generated method stubmtts. setparameter (speechconstant. engine_type, "local"); MTTS. setparameter (speechsynthesizer. voice_name, "Xiaoyan"); MTTS. setparameter (speechsynthesizer. speed, "50"); MTTS. setparameter (speechsynthesizer. pitch, "50"); int code = MTTS. startspeaking ("KEDA xunfei speech synthesis", mttslistener) ;}} private initlistener mttsinitlistener = new initlistener () {@ overridepublic void oninit (ispeechmodule arg0, int code) {// todo auto-generated method stub}; synthesizerlistener mttslistener = new synthesizerlistener. stub () {@ overridepublic void onspeakresumed () throws RemoteException {// todo auto-generated method stub} @ overridepublic void onspeakprogress (INT progress) throws RemoteException {// todo auto-generated method stub} @ overridepublic void onspeakpaused () throws RemoteException {// todo auto-generated method stub} @ overridepublic void onspeakbegin () throws RemoteException {// todo auto-generated method stub} @ overridepublic void oncompleted (INT code) throws RemoteException {// todo auto-generated method stub} @ overridepublic void onbufferprogress (INT progress) throws RemoteException {// todo auto-generated method stub }};}
Note: First paste the speechapi. jar file in the/libs directory.
Import the existing speechapi. jar file to the project.