I have a WP8 app, which uses the UI-less version of the SpeechRecognizer
. I use the following code:
string[] commands = { "Shoot" };
recognizer = new SpeechRecognizer();
await speechOutput.SpeakTextAsync("You can say Shoot.");
recognizer.Grammars.AddGrammarFromList("captureCommands", commands);
SpeechRecognitionResult result = await recognizer.RecognizeAsync();
if (result.Text == "Shoot")
cam.CaptureImage();
This prompts the user in both speech and on-screen text that he can say "Shoot" to take a photo. Three other options are:
- Tap the screen to shoot
- Click the shoot button in the
ApplicationBar
- Click the a button on the
ApplicationBar
to choose from the photo library
In all three cases I would like to cancel the speech recognizer task.
I tried removing the await
and casting the IAsyncOperation<SpeechRecognitionResult>
to a Task<SpeechRecognitionResult>
, but since I don't create the task and the method does not take a cancellation token, I don't know how to cancel it, even now that I have the task object.
If I don't cancel the task and navigate away from the page, I get an App level exception. Is there a way I can explicitly cancel the task or awaited operation?