Scene: After entering the program in the voice wake state, when speaking of a keyword to open a sub-interface (such as: Speech recognition interface)
Technical points:
1.
Set wake up until Stoplistening is called, incoming 0 completes a wake-up session immediately ends (default 0)
Mivw.setparameter (Speechconstant.keep_alive, "1");
2. Add resource file Appid.jet It's weird why there's no need for a grammar file in this demo.
Key code:
/********************************* Voice Wake-up ************************************************/ //Voice Wake-up Object PrivateVoicewakeuper MIVW; //wake-up result content PrivateString resultstring; Private voidInitawake () {stringbuffer param=NewStringBuffer (); String Respath= Resourceutil.generateresourcepath (mainactivity. This, Resource_type.assets, "ivw/" + getString (r.string.app_id) + ". Jet"); Param.append (Resourceutil.ivw_res_path+ "=" +Respath); Param.append ("," + resourceutil.engine_start + "=" +SPEECHCONSTANT.ENG_IVW); BooleanRET =speechutility.getutility (). Setparameter (Resourceutil.engine_start, param.tostring ()); if(!ret) {LOG.D (TAG,"Failed to start the local engine!" "); } //Initialize Wake-up ObjectMIVW = Voicewakeuper.createwakeuper ( This,NULL); //non-null judgment to prevent the program from crashing due to null pointersMIVW =Voicewakeuper.getwakeuper (); if(MIVW! =NULL) {//resultstring = "";//Textview.settext (resultstring); //Empty ParametersMivw.setparameter (Speechconstant.params,NULL); //wake Threshold, based on the number of wake-up words carried by the resource in the format "ID: threshold; ID: Threshold"Mivw.setparameter (Speechconstant.ivw_threshold, "0:" + 0);//20 is the gate value//Set Wake modeMivw.setparameter (Speechconstant.ivw_sst, "wakeup"); //set wake up until Stoplistening is called, incoming 0 completes a wake-up session immediately ends (default 0)Mivw.setparameter (Speechconstant.keep_alive, "1");//mivw.startlistening (mwakeuperlistener);//operation in Onresume}Else{Showtip ("Wake not Initialized"); } } PrivateWakeuperlistener Mwakeuperlistener =NewWakeuperlistener () {@Override Public voidOnresult (wakeuperresult result) {Try{String text=result.getresultstring (); Resultstring=text; Jsonobject object; Object=Newjsonobject (text); String Awakeid= object.optstring ("id"); if(!Stringutil.isnullorempty (Awakeid)) { //Open the speech recognition interface BooleanNeedshowvoicetips = (Boolean) sputils.get (mainactivity. This, Getresources (). getString (R.string.needshowusevoicetips),true); LOG.V (TAG,"Isshowvoicetips=" +needshowvoicetips); MIVW=Voicewakeuper.getwakeuper (); if(MIVW! =NULL) {mivw.stoplistening (); MIVW=NULL; } Else{Showtip ("Wake not Initialized"); } if(!needshowvoicetips) { if(voicepop==NULL||!voicepop.isshowing ()) {Voicepop=NewVoiceinputpopview (mainactivity. This); //Display windowVoicepop.showatlocation (iv_setting, gravity.center, 0, 0);//To set the location that layout displays in Popupwindow } } Else{Voidtipspop=NewUsevoicetipspop (mainactivity. This); //Display windowVoidtipspop.showatlocation (iv_setting, gravity.center, 0, 0);//To set the location that layout displays in Popupwindow } } } Catch(jsonexception e) {resultstring= "Result parsing error"; E.printstacktrace (); } Catch(Exception e) {e.printstacktrace (); }//Toast.maketext (mainactivity.this, "Results" +resultstring, Toast.length_short). Show ();} @Override Public voidonError (speecherror error) {Showtip (Error.getplaindescription (true)); } @Override Public voidOnbeginofspeech () {Showtip ("Try to say" The voice of the word "wake-up speech recognition interface."); } @Override Public voidOnEvent (intEventType,intIslast,intarg2, Bundle obj) { } };
Android Development Voice Wakeup function