c# - Cancel a Windows Phone 8 speech recognition session? -


i have windows phone 8 app uses speechrecognizer class (not speechrecognizerui) speech recognition. how cancel session in progress? don't see cancel or stop method in speechrecognizer class.

update: want give context mparkuk's answer based on msdn thread:

speechrecognizer.settings.initialsilencetimeout not working right

the way cancel speech recognition operation maintain reference iasyncoperation returned recognizer's iasyncoperation call instead of "discarding it" directly awaiting recoasync() call recognition result. including code below in case thread get's lost on time. gist of cancelling speech recognition session call iasyncoperation.cancel() method.

    private const int speechinputtimeoutmsec = 10000;      private speechrecognizer createrecognizerandloadgrammarasync(string filename, uri grammaruri, out task grammarloadtask)     {         // create recognizer , start loading grammars         speechrecognizer reco = new speechrecognizer();         // @@bugbug: set silence detection twice configured time - cancel thread         reco.settings.initialsilencetimeout = timespan.frommilliseconds(2 * speechinputtimeoutmsec);         reco.audiocapturestatechanged += recognizer_audiocapturestatechanged;         reco.grammars.addgrammarfromuri(filename, grammaruri);          // start pre-loading grammars minimize reco delays:         reco.preloadgrammarsasync();         return reco;     }      /// <summary>     /// recognize async     /// </summary>     public async void recognizeasync()     {         try         {             // start recognition asynchronously             this.currentrecooperation = this.recognizer.recognizeasync();              // @@bugbug: add protection code , handle speech timeout programmatically             this.speechbugworkaround_runhangprotectioncode(this.currentrecooperation);              // wait reco complete (or cancelled)             speechrecognitionresult result = await this.currentrecooperation;             this.currentrecooperation = null;      // results     results = getresults(result);             this.completerecognition(results, speecherror);         }         catch (exception ex)         {     // error             this.completerecognition(null, ex);         }          // restore recognizer next operation if necessary         this.reinitializerecogizerifnecessary();     }      private void speechbugworkaround_runhangprotectioncode(iasyncoperation<speechrecognitionresult> speechrecoop)     {         threadpool.queueuserworkitem(delegate(object s)         {             try             {                 bool cancelled = false;                 if (false == this.capturingevent.waitone(3000) && speechrecoop.status == asyncstatus.started)                 {                     cancelled = true;                     speechrecoop.cancel();                 }                  // if after 10 seconds still running - cancel operation.                 if (!cancelled)                 {                     thread.sleep(speechinputtimeoutmsec);                     if (speechrecoop.status == asyncstatus.started)                     {                         speechrecoop.cancel();                     }                 }             }             catch (exception) { /* todo: add exception handling code */}         }, null);     }      private void reinitializerecogizerifnecessary()     {         lock (this.sync)         {             // if audio capture event not raised, recognizer hang -> re-initialize it.             if (false == this.capturingevent.waitone(0))             {                 this.recognizer = null;                 this.createrecognizerandloadgrammarasync(...);             }         }     }      /// <summary>     /// handles audio capture events can tell ui thread listening...     /// </summary>     /// <param name="sender"></param>     /// <param name="args"></param>     private void recognizer_audiocapturestatechanged(speechrecognizer sender, speechrecognizeraudiocapturestatechangedeventargs args)     {         if (args.state == speechrecognizeraudiocapturestate.capturing)         {             this.capturingevent.set();          }     } 

------------------------------------- msdn thread -------------------

credit: mark chamberlain sr. escalation engineer | microsoft developer support | windows phone 8

regarding cancellation mechanism, here suggested code developer.

the iasyncoperation returned recognizeasync has cancel function.

you have to:

1) set initial silence timeout large value (e.g.: twice desired speech input timeout, in case 10 seconds) reco.settings.initialsilencetimeout = timespan.frommilliseconds(2 * speechinputtimeoutmsec);

2) store this.currentrecooperation = this.recognizer.recognizeasync();

3) kick off worker thread cancel operation after 10 seconds if necessary. didn’t want take chances added code re-initialize if hang detected. done looking @ whether audio capture state changed capturing within few seconds of starting recognition.

have seen thread?

speechrecognizer.settings.initialsilencetimeout not working right


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

android - Keyboard hides my half of edit-text and button below it even in scroll view -

css - Make div keyboard-scrollable in jQuery Mobile? -