SpeechRecognitionEngine.RecognizeAsyncStop Método
Definição
Importante
Algumas informações dizem respeito a um produto pré-lançado que pode ser substancialmente modificado antes de ser lançado. A Microsoft não faz garantias, de forma expressa ou implícita, em relação à informação aqui apresentada.
Para o reconhecimento assíncrono após a conclusão da operação de reconhecimento atual.
public:
void RecognizeAsyncStop();
public void RecognizeAsyncStop();
member this.RecognizeAsyncStop : unit -> unit
Public Sub RecognizeAsyncStop ()
Exemplos
O exemplo seguinte mostra parte de uma aplicação de consola que demonstra a utilização do RecognizeAsyncStop método. O exemplo cria e carrega uma gramática de reconhecimento de voz, inicia uma operação contínua de reconhecimento assíncrono e depois pausa 2 segundos antes de interromper a operação. O reconhecedor recebe entrada do ficheiro c:\temp\audioinput\sample.wav. Handlers de eventos são incluídos para demonstrar os eventos que o recognizer levanta durante a operação.
using System;
using System.Globalization;
using System.Speech.Recognition;
using System.Threading;
namespace AsynchronousRecognition
{
class Program
{
// Indicate whether asynchronous recognition is complete.
static bool completed;
static void Main(string[] args)
{
// Create an in-process speech recognizer.
using (SpeechRecognitionEngine recognizer =
new SpeechRecognitionEngine(new CultureInfo("en-US")))
{
// Create and load a dictation grammar.
Grammar dictation = new DictationGrammar();
dictation.Name = "Dictation Grammar";
recognizer.LoadGrammar(dictation);
// Attach event handlers.
recognizer.SpeechDetected +=
new EventHandler<SpeechDetectedEventArgs>(
SpeechDetectedHandler);
recognizer.SpeechHypothesized +=
new EventHandler<SpeechHypothesizedEventArgs>(
SpeechHypothesizedHandler);
recognizer.SpeechRecognitionRejected +=
new EventHandler<SpeechRecognitionRejectedEventArgs>(
SpeechRecognitionRejectedHandler);
recognizer.SpeechRecognized +=
new EventHandler<SpeechRecognizedEventArgs>(
SpeechRecognizedHandler);
recognizer.RecognizeCompleted +=
new EventHandler<RecognizeCompletedEventArgs>(
RecognizeCompletedHandler);
// Begin asynchronous recognition from pre-recorded input.
recognizer.SetInputToWaveFile(@"c:\temp\audioinput\sample.wav");
completed = false;
Console.WriteLine("Begin continuing asynchronous recognition...");
recognizer.RecognizeAsync(RecognizeMode.Multiple);
// Wait 2 seconds and then stop the recognition operation.
Thread.Sleep(TimeSpan.FromSeconds(2));
recognizer.RecognizeAsyncStop();
// Wait for the operation to complete.
while (!completed)
{
Thread.Sleep(333);
}
Console.WriteLine("Done.");
}
Console.WriteLine();
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
}
// Handle the SpeechDetected event.
static void SpeechDetectedHandler(object sender, SpeechDetectedEventArgs e)
{
Console.WriteLine(" In SpeechDetectedHandler:");
Console.WriteLine(" - AudioPosition = {0}", e.AudioPosition);
}
// Handle the SpeechHypothesized event.
static void SpeechHypothesizedHandler(
object sender, SpeechHypothesizedEventArgs e)
{
Console.WriteLine(" In SpeechHypothesizedHandler:");
string grammarName = "<not available>";
string resultText = "<not available>";
if (e.Result != null)
{
if (e.Result.Grammar != null)
{
grammarName = e.Result.Grammar.Name;
}
resultText = e.Result.Text;
}
Console.WriteLine(" - Grammar Name = {0}; Result Text = {1}",
grammarName, resultText);
}
// Handle the SpeechRecognitionRejected event.
static void SpeechRecognitionRejectedHandler(
object sender, SpeechRecognitionRejectedEventArgs e)
{
Console.WriteLine(" In SpeechRecognitionRejectedHandler:");
string grammarName = "<not available>";
string resultText = "<not available>";
if (e.Result != null)
{
if (e.Result.Grammar != null)
{
grammarName = e.Result.Grammar.Name;
}
resultText = e.Result.Text;
}
Console.WriteLine(" - Grammar Name = {0}; Result Text = {1}",
grammarName, resultText);
}
// Handle the SpeechRecognized event.
static void SpeechRecognizedHandler(
object sender, SpeechRecognizedEventArgs e)
{
Console.WriteLine(" In SpeechRecognizedHandler.");
string grammarName = "<not available>";
string resultText = "<not available>";
if (e.Result != null)
{
if (e.Result.Grammar != null)
{
grammarName = e.Result.Grammar.Name;
}
resultText = e.Result.Text;
}
Console.WriteLine(" - Grammar Name = {0}; Result Text = {1}",
grammarName, resultText);
}
// Handle the RecognizeCompleted event.
static void RecognizeCompletedHandler(
object sender, RecognizeCompletedEventArgs e)
{
Console.WriteLine(" In RecognizeCompletedHandler.");
if (e.Error != null)
{
Console.WriteLine(
" - Error occurred during recognition: {0}", e.Error);
return;
}
if (e.Cancelled)
{
Console.WriteLine(" - asynchronous operation canceled.");
}
if (e.InitialSilenceTimeout || e.BabbleTimeout)
{
Console.WriteLine(
" - BabbleTimeout = {0}; InitialSilenceTimeout = {1}",
e.BabbleTimeout, e.InitialSilenceTimeout);
return;
}
if (e.InputStreamEnded)
{
Console.WriteLine(
" - AudioPosition = {0}; InputStreamEnded = {1}",
e.AudioPosition, e.InputStreamEnded);
}
if (e.Result != null)
{
Console.WriteLine(
" - Grammar = {0}; Text = {1}; Confidence = {2}",
e.Result.Grammar.Name, e.Result.Text, e.Result.Confidence);
}
else
{
Console.WriteLine(" - No result.");
}
completed = true;
}
}
}
Observações
Este método finaliza o reconhecimento assíncrono sem truncar a entrada. Se a operação de reconhecimento assíncrona atual estiver a receber entrada, o reconhecor continua a aceitar entradas até que a operação de reconhecimento atual seja concluída. O reconhecedor levanta o RecognizeCompleted evento ou EmulateRecognizeCompleted quando uma operação assíncrona é parada, e define a Cancelled propriedade do RecognizeCompletedEventArgs para true. Este método impede operações assíncronas iniciadas pelos RecognizeAsync métodos e EmulateRecognizeAsync .
Para cancelar imediatamente o reconhecimento assíncrono apenas com a entrada existente, use o RecognizeAsyncCancel método.