快速入門:使用語音 SDK 在 macOS 上以 Objective-C 辨識語音Quickstart: Recognize speech in Objective-C on macOS using the Speech SDK

另備有語音合成的快速入門。Quickstarts are also available for speech synthesis.

在本文中,您將了解如何使用認知服務語音 SDK 以 Objective-C 建立 iOS 應用程式,以將麥克風錄製的語音轉譯成文字。In this article, you learn how to create a macOS app in Objective-C using the Cognitive Services Speech SDK to transcribe speech recorded from a microphone to text.


開始之前,以下為必要條件清單:Before you get started, here's a list of prerequisites:

取得適用於 macOS 的語音 SDKGet the Speech SDK for macOS


下載此頁面上的任何「Azure 認知服務的語音 SDK」元件,即表示您知悉其授權。By downloading any of the Speech SDK for Azure Cognitive Services components on this page, you acknowledge its license. 請參閱語音 SDK 的 Microsoft 軟體授權條款See the Microsoft Software License Terms for the Speech SDK.

適用於 Mac 的認知服務語音 SDK 會以架構組合的形式散發。The Cognitive Services Speech SDK for Mac is distributed as a framework bundle. 它可在 Xcode 專案中作為 CocoaPod 使用,或是從 https://aka.ms/csspeech/macosbinary 下載並手動連結。It can be used in Xcode projects as a CocoaPod, or downloaded from https://aka.ms/csspeech/macosbinary and linked manually. 本指南使用 CocoaPod。This guide uses a CocoaPod.

建立 Xcode 專案Create an Xcode project

啟動 Xcode,然後按一下 [檔案] > [新增] > [專案] 以啟動新專案。Start Xcode, and start a new project by clicking File > New > Project. 在範本選取對話方塊中,選擇「Cocoa 應用程式」範本。In the template selection dialog, choose the "Cocoa App" template.

在後續的對話方塊中,進行下列選取:In the dialogs that follow, make the following selections:

  1. 專案選項對話方塊Project Options Dialog
    1. 輸入快速入門應用程式的名稱,例如 helloworldEnter a name for the quickstart app, for example helloworld.
    2. 如果您已有 Apple 開發人員帳戶,請輸入適當的組織名稱和組織識別碼。Enter an appropriate organization name and an organization identifier, if you already have an Apple developer account. 基於測試用途,您可以直接挑選類似於 testorg 的任何名稱。For testing purposes, you can just pick any name like testorg. 若要簽署應用程式,您需要適當的佈建設定檔。To sign the app, you need a proper provisioning profile. 如需詳細資訊,請參閱 Apple 開發人員網站Refer to the Apple developer site for details.
    3. 確定已選擇 Objective-C 作為專案的語言。Make sure Objective-C is chosen as the language for the project.
    4. 停用這些核取方塊以使用分鏡腳本,並建立以文件為基礎的應用程式。Disable the checkboxes to use storyboards and to create a document-based application. 範例應用程式的簡單 UI 將會以程式設計方式建立。The simple UI for the sample app will be created programmatically.
    5. 停用測試和核心資料的所有核取方塊。Disable all checkboxes for tests and core data. 專案設定Project Settings
  2. 選取專案目錄Select project directory
    1. 選擇用來放置專案的目錄。Choose a directory to put the project in. 這會在您的主目錄中建立 helloworld 目錄,其中包含 Xcode 專案檔的所有檔案。This creates a helloworld directory in your home directory that contains all the files for the Xcode project.
    2. 停用為此範例專案建立 Git 存放庫的功能。Disable the creation of a Git repo for this example project.
  3. 設定網路和麥克風存取的權利。Set the entitlements for network and microphone access. 按一下左側概觀第一行中的應用程式名稱,以取得應用程式組態,然後選擇 [功能] 索引標籤。Click the app name in the first line in the overview on the left to get to the app configuration, and then choose the "Capabilities" tab.
    1. 啟用應用程式的 [應用程式沙箱] 設定。Enable the "App sandbox" setting for the app.
    2. 啟用 [連出連線] 和 [麥克風] 存取的核取方塊。Enable the checkboxes for "Outgoing Connections" and "Microphone" access. 沙箱設定Sandbox Settings
  4. 應用程式也必須在 Info.plist 檔案中宣告麥克風的使用。The app also needs to declare use of the microphone in the Info.plist file. 按一下概觀中的檔案,然後新增「隱私權 - 麥克風使用方式描述」索引鍵,並使用「語音辨識需要麥克風」之類的值。Click on the file in the overview, and add the "Privacy - Microphone Usage Description" key, with a value like "Microphone is needed for speech recognition". Info.plist 中的設定Settings in Info.plist
  5. 關閉 Xcode 專案。Close the Xcode project. 您後續在設定 CocoaPod 之後將使用其不同的執行個體。You will use a different instance of it later after setting up the CocoaPods.

將 SDK 安裝為 CocoaPodInstall the SDK as a CocoaPod

  1. 依照安裝指示中的說明安裝 CocoaPod 相依性管理員。Install the CocoaPod dependency manager as described in its installation instructions.
  2. 瀏覽至範例應用程式的目錄 (helloworld)。Navigate to the directory of your sample app (helloworld). 在該目錄中放入名為 Podfile 的文字檔和下列內容:Place a text file with the name Podfile and the following content in that directory:
    target 'helloworld' do
      platform :osx, '10.13'
      pod 'MicrosoftCognitiveServicesSpeech-macOS', '~> 1.7.0'
  3. 瀏覽至終端機的 helloworld 目錄,並執行命令 pod installNavigate to the helloworld directory in a terminal and run the command pod install. 這將會產生一個 helloworld.xcworkspace Xcode 工作區,其中包含範例應用程式和作為相依性的語音 SDK。This will generate a helloworld.xcworkspace Xcode workspace containing both the sample app and the Speech SDK as a dependency. 此工作區將用於下列作業。This workspace will be used in the following.

新增範例程式碼Add the sample code

  1. 在 XCode 中開啟 helloworld.xcworkspace 工作區。Open the helloworld.xcworkspace workspace in Xcode.
  2. 將自動產生的 AppDelegate.m 檔案內容取代為:Replace the contents of the autogenerated AppDelegate.m file by:
    #import "AppDelegate.h"
    #import <MicrosoftCognitiveServicesSpeech/SPXSpeechApi.h>
    @interface AppDelegate ()
    @property (weak) IBOutlet NSWindow *window;
    @property (weak) NSButton *button;
    @property (strong) NSTextField *label;
    @implementation AppDelegate
    - (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
        self.button = [NSButton buttonWithTitle:@"Start Recognition" target:nil action:nil];
        [self.button setTarget:self];
        [self.button setAction:@selector(buttonPressed:)];
        [self.window.contentView addSubview:self.button];
        self.label = [[NSTextField alloc] initWithFrame:NSMakeRect(100, 100, 200, 17)];
        [self.label setStringValue:@"Press Button!"];
        [self.window.contentView addSubview:self.label];
    - (void)buttonPressed:(NSButton *)button {
        // 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").
        NSString *speechKey = @"YourSubscriptionKey";
        NSString *serviceRegion = @"YourServiceRegion";
        SPXAudioConfiguration *audioConfig = [[SPXAudioConfiguration alloc] initWithMicrophone:nil];
        SPXSpeechConfiguration *speechConfig = [[SPXSpeechConfiguration alloc] initWithSubscription:speechKey region:serviceRegion];
        SPXSpeechRecognizer *speechRecognizer = [[SPXSpeechRecognizer alloc] initWithSpeechConfiguration:speechConfig audioConfiguration:audioConfig];
        NSLog(@"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 maximum of 15
        // seconds of audio is processed.  The task returns the recognition text as result.
        // Note: Since recognizeOnce() 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.
        SPXSpeechRecognitionResult *speechResult = [speechRecognizer recognizeOnce];
        // Checks result.
        if (SPXResultReason_Canceled == speechResult.reason) {
            SPXCancellationDetails *details = [[SPXCancellationDetails alloc] initFromCanceledRecognitionResult:speechResult];
            NSLog(@"Speech recognition was canceled: %@. Did you pass the correct key/region combination?", details.errorDetails);
            [self.label setStringValue:([NSString stringWithFormat:@"Canceled: %@", details.errorDetails])];
        } else if (SPXResultReason_RecognizedSpeech == speechResult.reason) {
            NSLog(@"Speech recognition result received: %@", speechResult.text);
            [self.label setStringValue:(speechResult.text)];
        } else {
            NSLog(@"There was an error.");
            [self.label setStringValue:(@"Speech Recognition Error")];
    - (void)applicationWillTerminate:(NSNotification *)aNotification {
        // Insert code here to tear down your application
    - (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication {
        return YES;
  3. 將字串 YourSubscriptionKey 取代為您的訂用帳戶金鑰。Replace the string YourSubscriptionKey with your subscription key.
  4. 以與您的訂用帳戶 (例如,免費試用訂用帳戶的 westus) 相關聯的區域取代 YourServiceRegion 字串。Replace the string YourServiceRegion with the region associated with your subscription (for example, westus for the free trial subscription).

建置並執行範例Build and run the sample

  1. 顯示偵錯輸出 ([檢視] > [偵錯區域] > [啟動主控台] )。Make the debug output visible (View > Debug Area > Activate Console).
  2. 建置範例程式碼,然後從功能表中選取 [產品] > [執行] 或按一下 [播放] 按鈕,以執行該程式碼。Build and run the example code by selecting Product > Run from the menu or clicking the Play button.
  3. 按一下該按鈕並說出幾個字後,您應該會在畫面的下半部看到說出的文字。After you click the button and say a few words, you should see the text you have spoken on the lower part of the screen. 當您第一次執行應用程式時,系統應會提示您為應用程式授與電腦麥克風的存取權。When you run the app for the first time, you should be prompted to give the app access to your computer's microphone.

後續步驟Next steps