共用方式為


語音辨識

使用語音辨識來提供輸入、指定動作或指令,並完成任務。

重要 APIWindows.Media.SpeechRecognition

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

設定語音辨識

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

若要自動以系統對話框請求存取及使用麥克風音訊訊號的權限(以下語音辨識與語音合成範例),只需在應用程式套件清單中設定麥克風裝置功能即可。 欲了解更多細節,請參閱應用程式能力宣告。

麥克風存取的隱私政策

如果使用者點擊「是」以授權麥克風存取,您的應用程式會被加入設定 -> 隱私 -> 麥克風頁面的核准應用程式清單。 不過,使用者可以隨時選擇關閉此設定,因此在嘗試使用前,應確認應用程式是否能存取麥克風。

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

這段片段展示了你的應用程式如何檢查麥克風是否存在,以及是否有使用權限。

public class AudioCapturePermissions
{
    // If no microphone is present, an exception is thrown with the following HResult value.
    private static int NoCaptureDevicesHResult = -1072845856;

    /// <summary>
    /// Note that this method only checks the Settings->Privacy->Microphone setting, it does not handle
    /// the Cortana/Dictation privacy check.
    ///
    /// You should perform this check every time the app gets focus, in case the user has changed
    /// the setting while the app was suspended or not in focus.
    /// </summary>
    /// <returns>True, if the microphone is available.</returns>
    public async static Task<bool> RequestMicrophonePermission()
    {
        try
        {
            // Request access to the audio capture device.
            MediaCaptureInitializationSettings settings = new MediaCaptureInitializationSettings();
            settings.StreamingCaptureMode = StreamingCaptureMode.Audio;
            settings.MediaCategory = MediaCategory.Speech;
            MediaCapture capture = new MediaCapture();

            await capture.InitializeAsync(settings);
        }
        catch (TypeLoadException)
        {
            // Thrown when a media player is not available.
            var messageDialog = new Windows.UI.Popups.MessageDialog("Media player components are unavailable.");
            await messageDialog.ShowAsync();
            return false;
        }
        catch (UnauthorizedAccessException)
        {
            // Thrown when permission to use the audio capture device is denied.
            // If this occurs, show an error or disable recognition functionality.
            return false;
        }
        catch (Exception exception)
        {
            // Thrown when an audio capture device is not present.
            if (exception.HResult == NoCaptureDevicesHResult)
            {
                var messageDialog = new Windows.UI.Popups.MessageDialog("No Audio Capture devices are present on this system.");
                await messageDialog.ShowAsync();
                return false;
            }
            else
            {
                throw;
            }
        }
        return true;
    }
}
/// <summary>
/// Note that this method only checks the Settings->Privacy->Microphone setting, it does not handle
/// the Cortana/Dictation privacy check.
///
/// You should perform this check every time the app gets focus, in case the user has changed
/// the setting while the app was suspended or not in focus.
/// </summary>
/// <returns>True, if the microphone is available.</returns>
IAsyncOperation<bool>^  AudioCapturePermissions::RequestMicrophonePermissionAsync()
{
    return create_async([]() 
    {
        try
        {
            // Request access to the audio capture device.
            MediaCaptureInitializationSettings^ settings = ref new MediaCaptureInitializationSettings();
            settings->StreamingCaptureMode = StreamingCaptureMode::Audio;
            settings->MediaCategory = MediaCategory::Speech;
            MediaCapture^ capture = ref new MediaCapture();

            return create_task(capture->InitializeAsync(settings))
                .then([](task<void> previousTask) -> bool
            {
                try
                {
                    previousTask.get();
                }
                catch (AccessDeniedException^)
                {
                    // Thrown when permission to use the audio capture device is denied.
                    // If this occurs, show an error or disable recognition functionality.
                    return false;
                }
                catch (Exception^ exception)
                {
                    // Thrown when an audio capture device is not present.
                    if (exception->HResult == AudioCapturePermissions::NoCaptureDevicesHResult)
                    {
                        auto messageDialog = ref new Windows::UI::Popups::MessageDialog("No Audio Capture devices are present on this system.");
                        create_task(messageDialog->ShowAsync());
                        return false;
                    }

                    throw;
                }
                return true;
            });
        }
        catch (Platform::ClassNotRegisteredException^ ex)
        {
            // Thrown when a media player is not available. 
            auto messageDialog = ref new Windows::UI::Popups::MessageDialog("Media Player Components unavailable.");
            create_task(messageDialog->ShowAsync());
            return create_task([] {return false; });
        }
    });
}
var AudioCapturePermissions = WinJS.Class.define(
    function () { }, {},
    {
        requestMicrophonePermission: function () {
            /// <summary>
            /// Note that this method only checks the Settings->Privacy->Microphone setting, it does not handle
            /// the Cortana/Dictation privacy check.
            ///
            /// You should perform this check every time the app gets focus, in case the user has changed
            /// the setting while the app was suspended or not in focus.
            /// </summary>
            /// <returns>True, if the microphone is available.</returns>
            return new WinJS.Promise(function (completed, error) {

                try {
                    // Request access to the audio capture device.
                    var captureSettings = new Windows.Media.Capture.MediaCaptureInitializationSettings();
                    captureSettings.streamingCaptureMode = Windows.Media.Capture.StreamingCaptureMode.audio;
                    captureSettings.mediaCategory = Windows.Media.Capture.MediaCategory.speech;

                    var capture = new Windows.Media.Capture.MediaCapture();
                    capture.initializeAsync(captureSettings).then(function () {
                        completed(true);
                    },
                    function (error) {
                        // Audio Capture can fail to initialize if there's no audio devices on the system, or if
                        // the user has disabled permission to access the microphone in the Privacy settings.
                        if (error.number == -2147024891) { // Access denied (microphone disabled in settings)
                            completed(false);
                        } else if (error.number == -1072845856) { // No recording device present.
                            var messageDialog = new Windows.UI.Popups.MessageDialog("No Audio Capture devices are present on this system.");
                            messageDialog.showAsync();
                            completed(false);
                        } else {
                            error(error);
                        }
                    });
                } catch (exception) {
                    if (exception.number == -2147221164) { // REGDB_E_CLASSNOTREG
                        var messageDialog = new Windows.UI.Popups.MessageDialog("Media Player components not available on this system.");
                        messageDialog.showAsync();
                        return false;
                    }
                }
            });
        }
    })

辨識語音輸入

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

你可以使用以下類型的限制來辨識語音輸入。

預定義文法

預設的語音輸入和網路搜尋文法能為你的應用程式提供語音辨識,且不需要你自行撰寫文法。 使用這些文法時,語音辨識由遠端網路服務執行,結果會回傳到裝置。

預設的自由文本聽寫語法能辨識使用者在特定語言中能說出的大多數單字和片語,並優化以辨識短語。 如果你沒有為 SpeechRecognitionr 物件指定任何限制,則會使用預設的語音輸入文法。 當你不想限制使用者能說的話時,自由文字聽寫很有用。 典型用途包括製作筆記或口述訊息內容。

網路搜尋文法和聽寫文法一樣,包含大量使用者可能說出的單字和片語。 然而,它優化了辨識人們在網路搜尋時常用的詞彙。

備註

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

這些預設文法可用來辨識長達 10 秒的語音輸入,且不需你額外撰寫。 不過,它們確實需要連接到網路。

若要使用網路服務限制,必須在 設定 中啟用語音輸入與語音輸入支援,方法是在 設定 -> 隱私 -> 語音、墨線與打字中開啟「認識我」選項。

在這裡,我們示範如何測試語音輸入是否啟用,並開啟設定 -> 隱私 -> 語音、描線與打字頁面(若未啟用)。

首先,我們將一個全域變數(HResultPrivacyStatementDeclined)初始化為 HResult 值 0x80045509。 請參考 C# 或 Visual Basic 中的例外處理

private static uint HResultPrivacyStatementDeclined = 0x80045509;

接著在識別過程中捕捉任何標準例外,並測試 HResult 值是否等於 HResultPrivacyStatementDeclined 變數的值。 如果是這樣,我們會顯示警告並呼叫 await Windows.System.Launcher.LaunchUriAsync(new Uri("ms-settings:privacy-accounts")); 開啟設定頁面。

catch (Exception exception)
{
  // Handle the speech privacy policy error.
  if ((uint)exception.HResult == HResultPrivacyStatementDeclined)
  {
    resultTextBlock.Visibility = Visibility.Visible;
    resultTextBlock.Text = "The privacy statement was declined." + 
      "Go to Settings -> Privacy -> Speech, inking and typing, and ensure you" +
      "have viewed the privacy policy, and 'Get To Know You' is enabled.";
    // Open the privacy/speech, inking, and typing settings page.
    await Windows.System.Launcher.LaunchUriAsync(new Uri("ms-settings:privacy-accounts")); 
  }
  else
  {
    var messageDialog = new Windows.UI.Popups.MessageDialog(exception.Message, "Exception");
    await messageDialog.ShowAsync();
  }
}

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

程式化清單限制

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

清單限制由一組字串組成,代表你的應用程式會接受用於辨識操作的語音輸入。 你可以在應用程式中建立一個語音辨識的清單約束物件,並傳遞一組字串陣列來建立清單限制。 接著,將該物件加入識別器的限制集合中。 當語音識別器能辨識陣列中任一字串時,即為成功。

參見 SpeechRecognitionListConstraint

SRGS 文法

語音辨識文法規範(SRGS)文法是一種靜態文件,與程式列表限制不同,它使用 SRGS 1.0 版本所定義的 XML 格式。 SRGS 文法能讓你在單一辨識中捕捉多種語意,提供對語音辨識體驗最大的控制。

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

語音指令限制

使用語音指令定義(VCD)XML 檔案來定義使用者在啟動應用程式時可以說出的指令,以啟動應用程式時的動作。 更多細節請參考透過 Cortana 啟動帶有語音指令的前景應用程式

參見 語音識別語音指令定義約束/

附註 您所使用的限制類型取決於您想要創建的辨識體驗的複雜性。 任何一個都可能是特定識別任務的最佳選擇,你也可以在應用程式中找到各種限制的用途。 要開始使用限制條件,請參見定義自訂識別限制。

預設的通用 Windows 應用程式語音輸入文法能辨識語言中大多數的單字和短語。 當語音識別器物件在未自訂限制下實例化時,該功能會預設啟動。

在這個範例中,我們示範如何:

  • 建立語音辨識器。
  • 編譯預設的 UWP 應用程式限制條件(語音識別器的文法集中尚未新增任何語法)。
  • 開始聆聽語音時,請使用 RecognizeWithUIAsync 方法提供的基本識別介面與TTS回饋。 如果不需要預設介面,請使用 RecognizeAsync 方法。
private async void StartRecognizing_Click(object sender, RoutedEventArgs e)
{
    // Create an instance of SpeechRecognizer.
    var speechRecognizer = new Windows.Media.SpeechRecognition.SpeechRecognizer();

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

    // Start recognition.
    Windows.Media.SpeechRecognition.SpeechRecognitionResult speechRecognitionResult = await speechRecognizer.RecognizeWithUIAsync();

    // Do something with the recognition result.
    var messageDialog = new Windows.UI.Popups.MessageDialog(speechRecognitionResult.Text, "Text spoken");
    await messageDialog.ShowAsync();
}

自訂辨識介面

當你的應用程式嘗試呼叫 SpeechRecognizer.RecognizeWithUIAsync 進行語音辨識時,會依序顯示多個畫面。

如果你使用的是基於預定義語法(聽寫或網頁搜尋)的限制:

  • 收聽畫面。
  • 思考界面。
  • 「聽到你說」 的畫面,還是錯誤的畫面?

如果你使用的是基於單字或片語清單的限制,或是基於 SRGS 文法檔案的限制:

  • 收聽模式畫面。
  • 如果使用者說的話可以被解讀為多種可能結果,那個畫面會顯示「 你說了嗎 」。
  • 「聽到你說」畫面還是錯誤畫面。

下圖展示了語音辨識器在螢幕間流動的範例,該程式使用基於 SRGS 文法檔案的限制。 在這個例子中,語音辨識是成功的。

基於 SGRS 文法檔案的約束初始辨識畫面

基於 SGRS 文法檔案的約束中間識別畫面

基於 SGRS 文法文件的限制的最終識別界面

聆聽畫面可以提供應用程式能辨識的單字或片語範例。 在此,我們展示如何利用 SpeechRecognizerUIOptions 類別的屬性(透過呼叫 SpeechRecognizer.UIOptions 屬性取得)來自訂 聆聽 畫面的內容。

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

    // Listen for audio input issues.
    speechRecognizer.RecognitionQualityDegrading += speechRecognizer_RecognitionQualityDegrading;

    // Add a web search grammar to the recognizer.
    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);

    // Compile the constraint.
    await speechRecognizer.CompileConstraintsAsync();

    // Start recognition.
    Windows.Media.SpeechRecognition.SpeechRecognitionResult speechRecognitionResult = await speechRecognizer.RecognizeWithUIAsync();
    //await speechRecognizer.RecognizeWithUIAsync();

    // Do something with the recognition result.
    var messageDialog = new Windows.UI.Popups.MessageDialog(speechRecognitionResult.Text, "Text spoken");
    await messageDialog.ShowAsync();
}

範例