Bagikan melalui


SpeechRecognitionEngine.RecognizeAsync Metode

Definisi

Memulai operasi pengenalan ucapan asinkron.

Overload

RecognizeAsync()

Melakukan satu operasi pengenalan ucapan asinkron.

RecognizeAsync(RecognizeMode)

Melakukan satu atau beberapa operasi pengenalan ucapan asinkron.

Keterangan

Metode ini melakukan operasi pengenalan asinkron tunggal atau ganda. Pengenal melakukan setiap operasi terhadap tata bahasa pengenalan ucapan yang dimuat dan diaktifkan.

Selama panggilan ke metode ini, pengenal dapat menaikkan peristiwa berikut:

Untuk mengambil hasil operasi pengenalan asinkron, lampirkan penanganan aktivitas ke peristiwa pengenal SpeechRecognized . Recognizer memunculkan peristiwa ini setiap kali berhasil menyelesaikan operasi pengenalan sinkron atau asinkron. Jika pengenalan tidak berhasil, Result properti pada RecognizeCompletedEventArgs objek, yang dapat Anda akses di handler untuk RecognizeCompleted peristiwa, akan menjadi null.

Operasi pengenalan asinkron dapat gagal karena alasan berikut:

RecognizeAsync()

Sumber:
SpeechRecognitionEngine.cs
Sumber:
SpeechRecognitionEngine.cs
Sumber:
SpeechRecognitionEngine.cs

Melakukan satu operasi pengenalan ucapan asinkron.

public:
 void RecognizeAsync();
public void RecognizeAsync ();
member this.RecognizeAsync : unit -> unit
Public Sub RecognizeAsync ()

Contoh

Contoh berikut menunjukkan bagian dari aplikasi konsol yang menunjukkan pengenalan ucapan asinkron dasar. Contohnya membuat DictationGrammar, memuatnya ke dalam pengenal ucapan dalam proses, dan melakukan satu operasi pengenalan asinkron. Penanganan aktivitas disertakan untuk menunjukkan peristiwa yang diangkat pengenal selama operasi.

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 a grammar for choosing cities for a flight.  
        Choices cities = new Choices(new string[]   
        { "Los Angeles", "New York", "Chicago", "San Francisco", "Miami", "Dallas" });  

        GrammarBuilder gb = new GrammarBuilder();  
        gb.Append("I want to fly from");  
        gb.Append(cities);  
        gb.Append("to");  
        gb.Append(cities);  

        // Construct a Grammar object and load it to the recognizer.  
        Grammar cityChooser = new Grammar(gb);  
        cityChooser.Name = ("City Chooser");  
        recognizer.LoadGrammarAsync(cityChooser);  

        // 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);  

        // Assign input to the recognizer and start an asynchronous  
        // recognition operation.  
        recognizer.SetInputToDefaultAudioDevice();  

        completed = false;  
        Console.WriteLine("Starting asynchronous recognition...");  
        recognizer.RecognizeAsync();  

        // 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.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);  
        Console.WriteLine(" - AudioPosition = {0}", e.AudioPosition);  
      }  
      else  
      {  
        Console.WriteLine(" - No result.");  
      }  

      completed = true;  
    }  
  }  
}

Keterangan

Metode ini melakukan satu operasi pengenalan asinkron. Pengenal melakukan operasi terhadap tata bahasa pengenalan ucapan yang dimuat dan diaktifkan.

Selama panggilan ke metode ini, pengenal dapat menaikkan peristiwa berikut:

Untuk mengambil hasil operasi pengenalan asinkron, lampirkan penanganan aktivitas ke peristiwa pengenal SpeechRecognized . Recognizer memunculkan peristiwa ini setiap kali berhasil menyelesaikan operasi pengenalan sinkron atau asinkron. Jika pengenalan tidak berhasil, Result properti pada RecognizeCompletedEventArgs objek, yang dapat Anda akses di handler untuk RecognizeCompleted peristiwa, akan menjadi null.

Untuk melakukan pengenalan sinkron, gunakan salah Recognize satu metode .

Metode ini disimpan dalam tugas yang menampilkan semua pengecualian non-penggunaan yang dapat dilemparkan oleh rekan sinkron metode. Jika pengecualian disimpan ke dalam tugas yang dikembalikan, pengecualian tersebut akan dilemparkan saat tugas ditunggu. Pengecualian penggunaan, seperti ArgumentException, masih dilemparkan secara sinkron. Untuk pengecualian yang disimpan, lihat pengecualian yang dilemparkan oleh Recognize().

Lihat juga

Berlaku untuk

RecognizeAsync(RecognizeMode)

Sumber:
SpeechRecognitionEngine.cs
Sumber:
SpeechRecognitionEngine.cs
Sumber:
SpeechRecognitionEngine.cs

Melakukan satu atau beberapa operasi pengenalan ucapan asinkron.

public:
 void RecognizeAsync(System::Speech::Recognition::RecognizeMode mode);
public void RecognizeAsync (System.Speech.Recognition.RecognizeMode mode);
member this.RecognizeAsync : System.Speech.Recognition.RecognizeMode -> unit
Public Sub RecognizeAsync (mode As RecognizeMode)

Parameter

mode
RecognizeMode

Menunjukkan apakah akan melakukan satu atau beberapa operasi pengenalan.

Contoh

Contoh berikut menunjukkan bagian dari aplikasi konsol yang menunjukkan pengenalan ucapan asinkron dasar. Contohnya membuat DictationGrammar, memuatnya ke dalam pengenal ucapan dalam proses, dan melakukan beberapa operasi pengenalan asinkron. Operasi asinkron dibatalkan setelah 30 detik. Penanganan aktivitas disertakan untuk menunjukkan peristiwa yang diangkat pengenal selama operasi.

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 a grammar for choosing cities for a flight.  
        Choices cities = new Choices(new string[] { "Los Angeles", "New York", "Chicago", "San Francisco", "Miami", "Dallas" });  

        GrammarBuilder gb = new GrammarBuilder();  
        gb.Append("I want to fly from");  
        gb.Append(cities);  
        gb.Append("to");  
        gb.Append(cities);  

        // Construct a Grammar object and load it to the recognizer.  
        Grammar cityChooser = new Grammar(gb);  
        cityChooser.Name = ("City Chooser");  
        recognizer.LoadGrammarAsync(cityChooser);  

        // 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);  

        // Assign input to the recognizer and start asynchronous  
        // recognition.  
        recognizer.SetInputToDefaultAudioDevice();  

        completed = false;  
        Console.WriteLine("Starting asynchronous recognition...");  
        recognizer.RecognizeAsync(RecognizeMode.Multiple);  

        // Wait 30 seconds, and then cancel asynchronous recognition.  
        Thread.Sleep(TimeSpan.FromSeconds(30));  
        recognizer.RecognizeAsyncCancel();  

        // 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.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);  
        Console.WriteLine(" - AudioPosition = {0}", e.AudioPosition);  
      }  
      else  
      {  
        Console.WriteLine(" - No result.");  
      }  

      completed = true;  
    }  
  }  
}  

Keterangan

Jika mode adalah Multiple, recognizer terus melakukan operasi pengenalan asinkron sampai RecognizeAsyncCancel metode atau RecognizeAsyncStop dipanggil.

Selama panggilan ke metode ini, pengenal dapat menaikkan peristiwa berikut:

Untuk mengambil hasil operasi pengenalan asinkron, lampirkan penanganan aktivitas ke peristiwa pengenal SpeechRecognized . Recognizer memunculkan peristiwa ini setiap kali berhasil menyelesaikan operasi pengenalan sinkron atau asinkron. Jika pengenalan tidak berhasil, Result properti pada RecognizeCompletedEventArgs objek, yang dapat Anda akses di handler untuk RecognizeCompleted peristiwa, akan menjadi null.

Operasi pengenalan asinkron dapat gagal karena alasan berikut:

  • Ucapan tidak terdeteksi sebelum interval waktu habis kedaluwarsa untuk BabbleTimeout properti atau InitialSilenceTimeout .

  • Mesin pengenalan mendeteksi ucapan tetapi tidak menemukan kecocokan di salah satu objek yang dimuat dan diaktifkan Grammar .

Untuk melakukan pengenalan sinkron, gunakan salah Recognize satu metode .

Lihat juga

Berlaku untuk