SpeechRecognizer.RecognizeOnceAsync メソッド

定義

非同期操作として音声認識を開始します。

public System.Threading.Tasks.Task<Microsoft.CognitiveServices.Speech.SpeechRecognitionResult> RecognizeOnceAsync ();
member this.RecognizeOnceAsync : unit -> System.Threading.Tasks.Task<Microsoft.CognitiveServices.Speech.SpeechRecognitionResult>
Public Function RecognizeOnceAsync () As Task(Of SpeechRecognitionResult)

戻り値

認識操作を表すタスク。 タスクは の値を返します。 SpeechRecognitionResult

次の例では、音声認識エンジンを作成し、認識結果を取得して出力します。

public async Task SpeechSingleShotRecognitionAsync()
{
    // Creates an instance of a speech config with specified subscription key and region.
    // Replace with your own subscription key and service region (e.g., "westus").
    var config = SpeechConfig.FromSubscription("YourSubscriptionKey", "YourServiceRegion");

    // Creates a speech recognizer using microphone as audio input. Default language: en-us
    using (var recognizer = new SpeechRecognizer(config))
    {
        Console.WriteLine("Say something...");

        // Starts speech recognition, and returns after a single utterance is recognized. 
        // The end of a single utterance is determined by listening for silence at the end 
        // or until a timeout period has elapsed.  The task returns the
        // recognition text as result.
        //
        // Note: Since RecognizeOnceAsync() returns only a single utterance, 
        // it is suitable only for single shot recognition like command or query.
        // For long-running multi-utterance recognition, 
        // use StartContinuousRecognitionAsync() instead.

        var result = await recognizer.RecognizeOnceAsync();

        // Checks result.
        if (result.Reason == ResultReason.RecognizedSpeech)
        {
            Console.WriteLine($"RECOGNIZED: Text={result.Text}");
        }
        else if (result.Reason == ResultReason.NoMatch)
        {
            Console.WriteLine($"NOMATCH: Speech could not be recognized.");
        }
        else if (result.Reason == ResultReason.Canceled)
        {
            var cancellation = CancellationDetails.FromResult(result);
            Console.WriteLine($"CANCELED: Reason={cancellation.Reason}");

            if (cancellation.Reason == CancellationReason.Error)
            {
                Console.WriteLine($"CANCELED: ErrorCode={cancellation.ErrorCode}");
                Console.WriteLine($"CANCELED: ErrorDetails={cancellation.ErrorDetails}");
                Console.WriteLine($"CANCELED: Did you update the subscription info?");
            }
        }
    }
}

注釈

1 つの発話の末尾は、最後に無音をリッスンするか、タイムアウト期間が経過するまでリッスンすることによって決定されます。 このタスクは、**SpeechRecognitionResult.Text** で認識された音声を返します。

**StopContinuousRecognitionAsync** を呼び出して、フレーズが認識される前に認識を停止できます。

このメソッドは 1 つの発話のみを返すので、コマンドやクエリなどのシングル ショット認識にのみ適しています。 実行時間の長い複数発話認識の場合は、代わりに **StartContinuousRecognitionAsync** を使用します。

音声テキスト変換の概要」も参照してください。

適用対象