本文內容

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

必要條件Prerequisites

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

認知服務語音 SDK 目前的版本為 1.6.0。The current version of the Cognitive Services Speech SDK is 1.6.0.

適用於 iOS 的認知服務語音 SDK 目前以 Cocoa Framework 的形式散發。The Cognitive Services Speech SDK for iOS is currently distributed as a Cocoa Framework.您可以從這裡加以下載。It can be downloaded from here.請將檔案下載到您的主目錄。Download the file to your home directory.

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

專案選項對話方塊Project Options Dialog

輸入快速入門應用程式的名稱，例如 helloworld。Enter a name for the quickstart app, for example helloworld.

如果您已有 Apple 開發人員帳戶，請輸入適當的組織名稱和組織識別碼。Enter an appropriate organization name and 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.

確定已選擇 Objective-C 作為專案的語言。Make sure Objective-C is chosen as the language for the project.

停用測試和核心資料的所有核取方塊。Disable all checkboxes for tests and core data.

選取專案目錄Select project directory

選擇用來放置專案的主目錄。Choose your home 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.

停用為此範例專案建立 Git 存放庫的功能。Disable the creation of a Git repo for this example project.

在 [專案設定] 中調整 SDK 的路徑。Adjust the paths to the SDK in the Project Settings.

設定 UISet up the UI

範例應用程式將提供一個非常簡單的 UI：以檔案或是麥克風輸入啟動語音辨識的兩個按鈕，以及顯示結果的文字標籤。The example app will have a very simple UI: Two buttons to start speech recognition either from file or from microphone input, and a text label to display the result.此 UI 可在專案的 Main.storyboard 部分中設定。The UI is set up in the Main.storyboard part of the project.以滑鼠右鍵按一下專案樹狀結構的 Main.storyboard 項目，然後選取 [開啟形式...] > [原始程式碼] ，以開啟分鏡腳本的 XML 檢視。Open the XML view of the storyboard by right-clicking the Main.storyboard entry of the project tree and selecting Open As... > Source Code.請將自動產生的 XML 取代為此程式碼：Replace the autogenerated XML with this code:

新增範例程式碼Add the sample code

以滑鼠右鍵按一下連結，然後選擇 [另存目標...] ，以下載範例 wav 檔案。將 wav 檔案從 [搜尋工具] 視窗拖曳到 [專案] 檢視的根層級中，以將其新增為專案的資源。Download the sample wav file by right-clicking the link and choosing Save target as.... Add the wav file to the project as a resource by dragging it from a Finder window into the root level of the Project view.在下列對話方塊中按一下 [完成] ，而不變更設定。Click Finish in the following dialog without changing the settings.

將麥克風存取的要求。Add the request for microphone access.以滑鼠右鍵按一下專案樹狀結構的 Info.plist 項目，然後選取 [Open As...] (開啟為) > [Source Code] (原始程式碼)。Right-click the Info.plist entry of the project tree and select Open As... > Source Code.將以下幾行新增至 <dict> 區段中，然後儲存檔案。Add the following lines into the <dict> section and then save the file.

建置並執行範例Building and Running the Sample

在 [產品] > [目的地] 功能表中的清單中，選擇 iOS 模擬器或是連接到您開發電腦的 iOS 裝置，當做應用程式的目的地。Choose either the iOS simulator or an iOS device connected to your development machine as the destination for the app from the list in the Product > Destination menu.

建置範例程式碼，然後從功能表中選取 [產品] > [執行] 或按一下 [播放] 按鈕，在 iOS 模擬器中加以執行。Build and run the example code in the iOS simulator by selecting Product > Run from the menu or clicking the Play button.

按一下應用程式中的 [辨識 (檔案)] 按鈕後，應該會在螢幕的下半部看到音訊檔案的內容After you click the "Recognize (File)" button in the app, you should see the contents of the audio file "What's the weather like?"「天氣如何？」。on the lower part of the screen.

按一下應用程式中的 [辨識 (麥克風)] 按鈕，並說出幾個字後，應該會在螢幕的下半部看到說出的文字。After you click the "Recognize (Microphone)" button in the app and say a few words, you should see the text you have spoken on the lower part of the screen.