Windows 應用程式中的語音辨識

使用語音辨識來提供輸入、指定動作或指令,並在你的 Windows 應用程式 SDK 應用程式中完成任務。

Important

語音辨識需要 MSIX 套件的識別碼。 Windows.Media.SpeechRecognition這些 API 只有在你的應用程式以套件身份(打包或以外部位置打包)執行時才可用。 未封裝的應用程式無法使用這些 API。

主要 API

概觀

語音辨識包含語音執行時、用於程式設計的識別API、可用於語音輸入與網頁搜尋的現成文法,以及幫助使用者發現和使用語音辨識功能的預設系統介面。

設定語音辨識

為了支援語音辨識,使用者必須在裝置上連接並啟用麥克風,並接受 Microsoft 隱私政策,授權您的應用程式使用該麥克風。

在應用程式的套件清單中設定 麥克風 裝置功能,提示使用者麥克風存取權限。 欲了解更多資訊,請參閱應用程式能力宣告。

如果使用者授權存取,你的應用程式會出現在設定>>隱私麥克風的已批准應用程式清單中。 因為使用者可以隨時關閉此設定,請在嘗試使用前確認你的應用程式是否具備麥克風權限。

如果你也想支援語音辨識或其他語音辨識服務(例如主題限制中定義的預設文法),請確認線上語音辨識設定>>隱私語音)已啟用。

以下範例說明如何檢查麥克風是否存在,以及你的應用程式是否有使用該麥克風的權限。

public class AudioCapturePermissions
{
    private static int NoCaptureDevicesHResult = -1072845856;

    /// <summary>
    /// Checks whether the microphone is available and the app has permission to use it.
    /// Perform this check every time the app gets focus, because the user can change
    /// the setting while the app is suspended or not in focus.
    /// </summary>
    /// <returns>True if the microphone is available.</returns>
    public static async Task<bool> RequestMicrophonePermission()
    {
        try
        {
            var settings = new MediaCaptureInitializationSettings
            {
                StreamingCaptureMode = StreamingCaptureMode.Audio,
                MediaCategory = MediaCategory.Speech
            };
            var capture = new MediaCapture();
            await capture.InitializeAsync(settings);
        }
        catch (TypeLoadException)
        {
            // Media player components are not available.
            return false;
        }
        catch (UnauthorizedAccessException)
        {
            // Permission to use the audio capture device is denied.
            return false;
        }
        catch (Exception exception)
        {
            if (exception.HResult == NoCaptureDevicesHResult)
            {
                // No audio capture devices are present on this system.
                return false;
            }
            throw;
        }
        return true;
    }
}

辨識語音輸入

限制定義了應用程式在語音輸入中能辨識的詞彙和片語。 限制是語音辨識的核心,讓你的應用程式能更好地掌控辨識準確度。

你可以使用以下類型的限制。

預定義文法

預設的聽寫和網路搜尋文法提供語音辨識,且不需要你自己撰寫文法。 當你使用這些文法時,遠端網路服務會執行辨識並將結果回傳給裝置。

預設的自由文本聽寫語法能辨識使用者在特定語言中能說出的大多數單字和片語,並針對短語進行優化。 如果你沒有為 語音識別器指定任何限制,就會使用預設的語音輸入文法。

網路搜尋文法包含大量單字和片語,針對人們在網路上常用的詞彙進行優化。

備註

預設的語音輸入和網路搜尋文法可能很大,且因為它們是線上的(不在裝置上),效能可能不如本地安裝自訂文法。

這些預設文法可辨識最多 10 秒的語音輸入,且不需撰寫作業。 不過,它們需要網路連線。

參見 語音識別主題限制(SpeechRecognitionTopicConstraint)。

程式化清單限制

程式化清單限制提供了一種輕量化的方法,透過使用單字或片語清單來建立簡單的文法。 列表限制對於辨識短小且獨特的片語效果良好。 指定文法中的所有單字能提升辨識準確度,因為語音辨識引擎只需處理語音以確認匹配。 清單也可以程式化更新。

參見 語音識別ListConstraint

SRGS 文法

語音辨識文法規範(SRGS)文法是一種靜態文件,使用 SRGS 1.0 版所定義的 XML 格式。 SRGS 文法讓你能在單一辨識中捕捉多種語意,從而對語音辨識體驗有最大控制力。

請參見 語音識別文法檔案約束(SpeechRecognitionGrammarFileConstraint)。

語音指令限制

使用語音指令定義(VCD)XML 檔案來定義使用者在啟動應用程式時可以說出的指令,以啟動應用程式時的動作。 請參見 語音識別語音指令定義約束(SpeechRecognitionVoiceCommandDefinitionConstraint)。

要開始使用限制條件,請參見定義自訂識別限制。

基本辨識範例

以下範例建立語音識別器,編譯預設的語音輸入約束,並開始聆聽語音輸入。

private async void StartRecognizing_Click(object sender, RoutedEventArgs e)
{
    var speechRecognizer = new Windows.Media.SpeechRecognition.SpeechRecognizer();

    // Compile the default dictation grammar.
    await speechRecognizer.CompileConstraintsAsync();

    // Start recognition.
    Windows.Media.SpeechRecognition.SpeechRecognitionResult result =
        await speechRecognizer.RecognizeAsync();

    // Display the recognized text.
    if (result.Status == Windows.Media.SpeechRecognition.SpeechRecognitionResultStatus.Success)
    {
        resultTextBlock.Text = result.Text;
    }
}

備註

這個例子使用 RecognizeAsync (無 UI)。 如果你想使用內建的辨識介面,請直接打電話 RecognizeWithUIAsync 。 請參閱 SpeechRecognizerUIOptions 客製化選項。

自訂辨識介面

當你的應用程式呼叫 SpeechRecognizer.RecognizeWithUIAsync 時,系統會依序顯示多個畫面:

  • 針對預設文法(聽寫或網路搜尋): 聆聽思考聽到你說的話 (或錯誤)。
  • 關於列表或 SRGS 限制: 聆聽你說了 (如果有歧義)→ 聽到你說 了(或錯誤)。

使用 SpeechRecognizerUIOptions 類別(透過 SpeechRecognizer.UIOptions取得)來自訂 聆聽 畫面:

private async void WeatherSearch_Click(object sender, RoutedEventArgs e)
{
    var speechRecognizer = new Windows.Media.SpeechRecognition.SpeechRecognizer();

    speechRecognizer.RecognitionQualityDegrading += SpeechRecognizer_RecognitionQualityDegrading;

    var webSearchGrammar = new Windows.Media.SpeechRecognition.SpeechRecognitionTopicConstraint(
        Windows.Media.SpeechRecognition.SpeechRecognitionScenario.WebSearch, "webSearch");

    speechRecognizer.UIOptions.AudiblePrompt = "Say what you want to search for...";
    speechRecognizer.UIOptions.ExampleText = @"Ex. 'weather for London'";
    speechRecognizer.Constraints.Add(webSearchGrammar);

    await speechRecognizer.CompileConstraintsAsync();

    Windows.Media.SpeechRecognition.SpeechRecognitionResult result =
        await speechRecognizer.RecognizeWithUIAsync();

    resultTextBlock.Text = result.Text;
}