快速入門:使用語音 SDK 在 UWP 應用程式中辨識語音Quickstart: Recognize speech in a UWP app by using the Speech SDK

另有語音合成語音翻譯語音優先虛擬助理的快速入門。Quickstarts are also available for speech synthesis, speech translation, and voice-first virtual assistant.

您也可以選擇不同的程式設計語言和/或環境:If you want, choose a different programming language and/or environment:

在此文章中,您會使用認知服務語音 SDK,來開發 C# 通用 Windows 平台 (UWP) 應用程式。In this article, you develop a C# Universal Windows Platform (UWP) application by using the Cognitive Services Speech SDK. 該程式會從您裝置的麥克風將語音即時轉換成文字。The program transcribes speech to text in real time from your device's microphone. 應用程式是利用語音 SDK NuGet 套件 (英文) 與 Microsoft Visual Studio 2019 (任何版本) 所建置的。The application is built with the Speech SDK NuGet Package and Microsoft Visual Studio 2019 (any edition).

注意

通用 Windows 平台可讓您開發應用程式,以在任何支援 Windows 10 的裝置 (包括 PC、Xbox、Surface Hub 與其他裝置) 上執行。The Universal Windows Platform lets you develop apps that run on any device that supports Windows 10, including PCs, Xbox, Surface Hub, and other devices.

必要條件Prerequisites

本快速入門需要:This quickstart requires:

建立 Visual Studio 專案Create a Visual Studio project

若要建立適用於通用 Windows 平台 (UWP) 開發的 Visual Studio 專案,您必須設定 Visual Studio 開發選項、建立專案、選取目標架構、設定音訊擷取,以及安裝語音 SDK。To create a Visual Studio project for Universal Windows Platform (UWP) development, you need to set up Visual Studio development options, create the project, select the target architecture, set up audio capture, and install the Speech SDK.

設定 Visual Studio 開發選項Set up Visual Studio development options

若要開始,請確定您已在適用於 UWP 開發的 Visual Studio 中正確設定:To start, make sure you're set up correctly in Visual Studio for UWP development:

  1. 開啟 Visual Studio 2019 以顯示 [開始] 視窗。Open Visual Studio 2019 to display the Start window.

    [開始] 視窗 - Visual Studio

  2. 選取 [不使用程式碼繼續] ,以移至 Visual Studio IDE。Select Continue without code to go to the Visual Studio IDE.

  3. 從 [Visual Studio] 功能表列中,選取 [工具] > [取得工具和功能] 以開啟 Visual Studio 安裝程式並檢視 [修改] 對話方塊。From the Visual Studio menu bar, select Tools > Get Tools and Features to open Visual Studio Installer and view the Modifying dialog box.

    [工作負載] 索引標籤、[修改] 對話方塊、Visual Studio 安裝程式

  4. 在 [工作負載] 索引標籤的 [Windows] 底下,尋找 [通用 Windows 平台開發] 工作負載。In the Workloads tab, under Windows, find the Universal Windows Platform development workload. 如果已選取該工作負載旁的核取方塊,請關閉 [修改] 對話方塊,然後移至步驟 6。If the check box next to that workload is already selected, close the Modifying dialog box, and go to step 6.

  5. 選取 [通用 Windows 平台開發] 核取方塊,選取 [修改] ,然後在 [開始使用之前] 對話方塊中,選取 [繼續] 以安裝 UWP 開發工作負載。Select the Universal Windows Platform development check box, select Modify, and then in the Before we get started dialog box, select Continue to install the UWP development workload. 安裝新功能可能需要一些時間。Installation of the new feature may take a while.

  6. 關閉 Visual Studio 安裝程式。Close Visual Studio Installer.

建立專案並選取目標架構Create the project and select the target architecture

接下來,建立您的專案:Next, create your project:

  1. 在 [Visual Studio] 功能表列中,選擇 [檔案] > [新增] > [專案] 以顯示 [建立新專案] 視窗。In the Visual Studio menu bar, choose File > New > Project to display the Create a new project window.

    建立新專案 - Visual Studio

  2. 尋找並選取 [空白應用程式 (通用 Windows)] 。Find and select Blank App (Universal Windows). 請確定您選取的是此專案類型的 C# 版本 (而不是 Visual Basic)。Make sure that you select the C# version of this project type (as opposed to Visual Basic).

  3. 選取 [下一步] 以顯示 [設定您的新專案] 畫面。Select Next to display the Configure your new project screen.

    設定您的新專案 - Visual Studio

  4. 在 [專案名稱] 中,輸入 helloworldIn Project name, enter helloworld.

  5. 在 [位置] 中,瀏覽至用來儲存專案的資料夾,然後選取或建立。In Location, navigate to and select or create the folder to save your project in.

  6. 選取 [建立] 以移至 [新增通用 Windows 平台專案] 視窗。Select Create to go to the New Universal Windows Platform Project window.

    [新增通用 Windows 平台專案] 對話方塊 - Visual Studio

  7. 在 [最小版本] (第二個下拉式方塊) 中,選擇 [Windows 10 Fall Creators Update (10.0; 組建 16299)] ,這是語音 SDK 的最低需求。In Minimum version (the second drop-down box), choose Windows 10 Fall Creators Update (10.0; Build 16299), which is the minimum requirement for the Speech SDK.

  8. 在 [目標版本] (第一個下拉式方塊) 中,選擇與 [最小版本] 中的值相同或之後的值。In Target version (the first drop-down box), choose a value identical to or later than the value in Minimum version.

  9. 選取 [確定] 。Select OK. 您會回到 Visual Studio IDE,新的專案會建立並顯示在 [方案總管] 窗格中。You're returned to the Visual Studio IDE, with the new project created and visible in the Solution Explorer pane.

    helloworld 專案 - Visual Studio

現在,請選取您的目標平台架構。Now select your target platform architecture. 在 [Visual Studio] 工具列中, 尋找 [方案平台] 下拉式方塊。In the Visual Studio toolbar, find the Solution Platforms drop-down box. (如果您沒有看到它,請選擇 [檢視] > [工具列] > [標準] 以顯示包含 [方案平台] 的工具列。)如果您執行的是 64 位元 Windows,請在下拉式方塊中選擇 [x64] 。(If you don't see it, choose View > Toolbars > Standard to display the toolbar containing Solution Platforms.) If you're running 64-bit Windows, choose x64 in the drop-down box. 64 位元 Windows 也可執行 32 位元應用程式,因此您可以依需求選擇 [x86] 。64-bit Windows can also run 32-bit applications, so you can choose x86 if you prefer.

注意

語音 SDK 僅支援 Intel 相容處理器。The Speech SDK only supports Intel-compatible processors. 目前不支援 ARM 處理器。ARM processors are currently not supported.

設定音訊擷取Set up audio capture

然後允許專案擷取音訊輸入:Then allow the project to capture audio input:

  1. 在 [方案總管] 中,按兩下 [Package.appxmanifest] 以開啟套件應用程式資訊清單。In Solution Explorer, double-click Package.appxmanifest to open the package application manifest.

  2. 選取 [功能] 索引標籤。Select the Capabilities tab.

    [功能] 索引標籤,套件應用程式資訊清單 - Visual Studio

  3. 選取 [麥克風] 功能的方塊。Select the box for the Microphone capability.

  4. 從功能表列中,選擇 [檔案] > [儲存 Package.appxmanifest] 來儲存變更。From the menu bar, choose File > Save Package.appxmanifest to save your changes.

安裝語音 SDKInstall the Speech SDK

最後,安裝語音 SDK NuGet 套件 (英文),並在您的專案中參考語音 SDK:Finally, install the Speech SDK NuGet package, and reference the Speech SDK in your project:

  1. 在 [方案總管] 中,以滑鼠右鍵按一下您的解決方案,然後選擇 [管理解決方案的 NuGet 套件] 以移至 [NuGet - 解決方案] 視窗。In Solution Explorer, right-click your solution, and choose Manage NuGet Packages for Solution to go to the NuGet - Solution window.

  2. 選取 [瀏覽] 。Select Browse.

    管理解決方案套件對話方塊的螢幕擷取畫面

  3. 在 [套件來源] 中,選擇 [nuget.org] 。In Package source, choose nuget.org.

  4. 在 [搜尋] 方塊中,輸入 Microsoft.CognitiveServices.Speech,然後在搜尋結果中出現該套件後加以選擇。In the Search box, enter Microsoft.CognitiveServices.Speech, and then choose that package after it appears in the search results.

    管理解決方案套件對話方塊的螢幕擷取畫面

  5. 在搜尋結果旁的 [套件狀態] 窗格中,選取您的 helloworld 專案。In the package status pane next to the search results, select your helloworld project.

  6. 選取 [安裝] 。Select Install.

  7. 在 [預覽變更] 對話方塊中,選取 [確定] 。In the Preview Changes dialog box, select OK.

  8. 在 [接受授權] 對話方塊中,檢視授權,然後選取 [我接受] 。In the License Acceptance dialog box, view the license, and then select I Accept. 套件安裝隨即開始,而且當安裝完成時,[輸出] 窗格會顯示類似下列文字的訊息:Successfully installed 'Microsoft.CognitiveServices.Speech 1.6.0' to helloworldThe package installation begins, and when installation is complete, the Output pane displays a message similar to the following text: Successfully installed 'Microsoft.CognitiveServices.Speech 1.6.0' to helloworld.

新增範例程式碼Add sample code

現在,新增可定義應用程式使用者介面的 XAML 程式碼,並新增程式 C# 程式碼後置實作。Now add the XAML code that defines the user interface of the application, and add the C# code-behind implementation.

  1. 在 [方案總管] 中開啟 MainPage.xamlIn Solution Explorer, open MainPage.xaml.

  2. 在設計工具的 XAML 檢視中,將下列 XAML 程式碼片段插入格線標記 (在 <Grid></Grid> 之間):In the designer's XAML view, insert the following XAML snippet into the Grid tag (between <Grid> and </Grid>):

    <StackPanel Orientation="Vertical" HorizontalAlignment="Center"  Margin="20,50,0,0" VerticalAlignment="Center" Width="800">
        <Button x:Name="EnableMicrophoneButton" Content="Enable Microphone"  Margin="0,0,10,0" Click="EnableMicrophone_ButtonClicked" Height="35"/>
        <Button x:Name="SpeechRecognitionButton" Content="Speech recognition with microphone input" Margin="0,10,10,0" Click="SpeechRecognitionFromMicrophone_ButtonClicked" Height="35"/>
        <StackPanel x:Name="StatusPanel" Orientation="Vertical" RelativePanel.AlignBottomWithPanel="True" RelativePanel.AlignRightWithPanel="True" RelativePanel.AlignLeftWithPanel="True">
            <TextBlock x:Name="StatusLabel" Margin="0,10,10,0" TextWrapping="Wrap" Text="Status:" FontSize="20"/>
            <Border x:Name="StatusBorder" Margin="0,0,0,0">
                <ScrollViewer VerticalScrollMode="Auto"  VerticalScrollBarVisibility="Auto" MaxHeight="200">
                    <!-- Use LiveSetting to enable screen readers to announce the status update. -->
                    <TextBlock x:Name="StatusBlock" FontWeight="Bold" AutomationProperties.LiveSetting="Assertive"
                    MaxWidth="{Binding ElementName=Splitter, Path=ActualWidth}" Margin="10,10,10,20" TextWrapping="Wrap"  />
                </ScrollViewer>
            </Border>
        </StackPanel>
    </StackPanel>
    
  3. 在 [方案總管] 中,開啟程式碼後置原始檔 MainPage.xaml.csIn Solution Explorer, open the code-behind source file MainPage.xaml.cs. (它分組在 MainPage.xaml 底下。)(It's grouped under MainPage.xaml.)

  4. 將其中的所有程式碼取代為下列程式碼片段:Replace all the code in it with the following snippet:

    using System;
    using System.Text;
    using Windows.UI.Xaml;
    using Windows.UI.Xaml.Controls;
    using Windows.UI.Xaml.Media;
    using Microsoft.CognitiveServices.Speech;
    
    namespace helloworld
    {
        /// <summary>
        /// An empty page that can be used on its own or navigated to within a Frame.
        /// </summary>
        public sealed partial class MainPage : Page
        {
            public MainPage()
            {
                this.InitializeComponent();
            }
    
            private async void EnableMicrophone_ButtonClicked(object sender, RoutedEventArgs e)
            {
                bool isMicAvailable = true;
                try
                {
                    var mediaCapture = new Windows.Media.Capture.MediaCapture();
                    var settings = new Windows.Media.Capture.MediaCaptureInitializationSettings();
                    settings.StreamingCaptureMode = Windows.Media.Capture.StreamingCaptureMode.Audio;
                    await mediaCapture.InitializeAsync(settings);
                }
                catch (Exception)
                {
                    isMicAvailable = false;
                }
                if (!isMicAvailable)
                {
                    await Windows.System.Launcher.LaunchUriAsync(new Uri("ms-settings:privacy-microphone"));
                }
                else
                {
                    NotifyUser("Microphone was enabled", NotifyType.StatusMessage);
                }
            }
    
            private async void SpeechRecognitionFromMicrophone_ButtonClicked(object sender, RoutedEventArgs e)
            {
                // Creates an instance of a speech config with specified subscription key and service region.
                // Replace with your own subscription key and service region (e.g., "westus").
                var config = SpeechConfig.FromSubscription("YourSubscriptionKey", "YourServiceRegion");
    
                try
                {
                    // Creates a speech recognizer using microphone as audio input.
                    using (var recognizer = new SpeechRecognizer(config))
                    {
                        // 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 maximum of 15
                        // seconds of audio is processed.  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().ConfigureAwait(false);
    
                        // Checks result.
                        StringBuilder sb = new StringBuilder();
                        if (result.Reason == ResultReason.RecognizedSpeech)
                        {
                            sb.AppendLine($"RECOGNIZED: Text={result.Text}");
                        }
                        else if (result.Reason == ResultReason.NoMatch)
                        {
                            sb.AppendLine($"NOMATCH: Speech could not be recognized.");
                        }
                        else if (result.Reason == ResultReason.Canceled)
                        {
                            var cancellation = CancellationDetails.FromResult(result);
                            sb.AppendLine($"CANCELED: Reason={cancellation.Reason}");
    
                            if (cancellation.Reason == CancellationReason.Error)
                            {
                                sb.AppendLine($"CANCELED: ErrorCode={cancellation.ErrorCode}");
                                sb.AppendLine($"CANCELED: ErrorDetails={cancellation.ErrorDetails}");
                                sb.AppendLine($"CANCELED: Did you update the subscription info?");
                            }
                        }
    
                        // Update the UI
                        NotifyUser(sb.ToString(), NotifyType.StatusMessage);
                    }
                }
                catch(Exception ex)
                {
                    NotifyUser($"Enable Microphone First.\n {ex.ToString()}", NotifyType.ErrorMessage);
                }
            }
    
            private enum NotifyType
            {
                StatusMessage,
                ErrorMessage
            };
    
            private void NotifyUser(string strMessage, NotifyType type)
            {
                // If called from the UI thread, then update immediately.
                // Otherwise, schedule a task on the UI thread to perform the update.
                if (Dispatcher.HasThreadAccess)
                {
                    UpdateStatus(strMessage, type);
                }
                else
                {
                    var task = Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => UpdateStatus(strMessage, type));
                }
            }
    
            private void UpdateStatus(string strMessage, NotifyType type)
            {
                switch (type)
                {
                    case NotifyType.StatusMessage:
                        StatusBorder.Background = new SolidColorBrush(Windows.UI.Colors.Green);
                        break;
                    case NotifyType.ErrorMessage:
                        StatusBorder.Background = new SolidColorBrush(Windows.UI.Colors.Red);
                        break;
                }
                StatusBlock.Text += string.IsNullOrEmpty(StatusBlock.Text) ? strMessage : "\n" + strMessage;
    
                // Collapse the StatusBlock if it has no text to conserve real estate.
                StatusBorder.Visibility = !string.IsNullOrEmpty(StatusBlock.Text) ? Visibility.Visible : Visibility.Collapsed;
                if (!string.IsNullOrEmpty(StatusBlock.Text))
                {
                    StatusBorder.Visibility = Visibility.Visible;
                    StatusPanel.Visibility = Visibility.Visible;
                }
                else
                {
                    StatusBorder.Visibility = Visibility.Collapsed;
                    StatusPanel.Visibility = Visibility.Collapsed;
                }
                // Raise an event if necessary to enable a screen reader to announce the status update.
                var peer = Windows.UI.Xaml.Automation.Peers.FrameworkElementAutomationPeer.FromElement(StatusBlock);
                if (peer != null)
                {
                    peer.RaiseAutomationEvent(Windows.UI.Xaml.Automation.Peers.AutomationEvents.LiveRegionChanged);
                }
            }
        }
    }
    
  5. 在來源檔案的 SpeechRecognitionFromMicrophone_ButtonClicked 處理常式中,尋找字串 YourSubscriptionKey,並將其取代為與您的訂用帳戶金鑰相關聯的區域。In the source file's SpeechRecognitionFromMicrophone_ButtonClicked handler, find the string YourSubscriptionKey, and replace it with your subscription key.

  6. SpeechRecognitionFromMicrophone_ButtonClicked 處理常式中,尋找字串 YourServiceRegion,並將它取代為與您的訂用帳戶相關聯的區域In the SpeechRecognitionFromMicrophone_ButtonClicked handler, find the string YourServiceRegion, and replace it with the region associated with your subscription. (例如,針對免費試用訂用帳戶使用 westus。)(For example, use westus for the free trial subscription.)

  7. 從功能表列中,選擇 [檔案] > [全部儲存] 來儲存變更。From the menu bar, choose File > Save All to save your changes.

建置並執行應用程式Build and run the application

現在,您可以開始建置和測試應用程式。Now you are ready to build and test your application.

  1. 從功能表列中,選擇 [建置] > [建置方案] 來建置應用程式。From the menu bar, choose Build > Build Solution to build the application. 現在會編譯程式碼,而且不會出現任何錯誤。The code should compile without errors now.

  2. 選擇 [偵錯] > [開始偵錯] (或按 F5) 以啟動應用程式。Choose Debug > Start Debugging (or press F5) to start the application. helloworld 視窗會出現。The helloworld window appears.

    C# 範例 UWP 語音辨識應用程式 - 快速入門

  3. 選取 [啟用麥克風] ,當存取權限要求出現時,選取 [是] 。Select Enable Microphone, and when the access permission request pops up, select Yes.

    麥克風存取權限要求

  4. 選取 [使用麥克風輸入的語音辨識] ,然後對裝置的麥克風說出英文片語或句子。Select Speech recognition with microphone input, and speak an English phrase or sentence into your device's microphone. 您的語音會傳送到語音服務,並且轉譯為文字,出現在視窗中。Your speech is transmitted to the Speech Services and transcribed to text, which appears in the window.

    語音辨識使用者介面

後續步驟Next steps

另請參閱See also