快速入門:使用語音 SDK 在 iOS 上以 Swift 辨識語音Quickstart: Recognize speech in Swift on iOS using the Speech SDK

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

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

必要條件Prerequisites

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

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

重要

下載此頁面上的任何「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.

請注意,本教學課程不適用於 1.6.0 之前的 SDK 版本。Note that this tutorial will not work with version of the SDK earlier than 1.6.0.

適用於 iOS 的認知服務語音 SDK 會以架構組合的形式散發。The Cognitive Services Speech SDK for iOS 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. 在範本選取對話方塊中,選擇「iOS 單一檢視應用程式」範本。In the template selection dialog, choose the "iOS Single View 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. 確定已選擇 Swift 作為專案的語言。Make sure Swift 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.
  2. 選取專案目錄Select project directory
    1. 選擇用來放置專案的目錄。Choose a directory to put the project in. 這會在您選擇的目錄中建立 helloworld 目錄,其中包含 Xcode 專案的所有檔案。This creates a helloworld directory in the chosen directory that contains all the files for the Xcode project.
    2. 停用為此範例專案建立 Git 存放庫的功能。Disable the creation of a Git repo for this example project.
  3. 應用程式也必須在 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
  4. 關閉 Xcode 專案。Close the Xcode project. 您後續在設定 CocoaPod 之後將使用其不同的執行個體。You will use a different instance of it later after setting up the CocoaPods.

新增範例程式碼Add the sample code

  1. 在 helloworld 專案內的 helloworld 目錄中放入名為 MicrosoftCognitiveServicesSpeech-Bridging-Header.h 的新標頭檔,並貼入下列程式碼:Place a new header file with the name MicrosoftCognitiveServicesSpeech-Bridging-Header.h into the helloworld directory inside the helloworld project, and paste the following code into it:
    #ifndef MicrosoftCognitiveServicesSpeech_Bridging_Header_h
    #define MicrosoftCognitiveServicesSpeech_Bridging_Header_h
    
    #import <MicrosoftCognitiveServicesSpeech/SPXSpeechAPI.h>
    
    #endif /* MicrosoftCognitiveServicesSpeech_Bridging_Header_h */
    
  2. 在 [Objective-C 橋接標頭] 欄位中,將橋接標頭的相對路徑 helloworld/MicrosoftCognitiveServicesSpeech-Bridging-Header.h 新增至 helloworld 目標的 Swift 的專案設定。標頭屬性Add the relative path helloworld/MicrosoftCognitiveServicesSpeech-Bridging-Header.h to the bridging header to the Swift project settings for the helloworld target in the Objective-C Bridging Header field Header properties
  3. 將自動產生的 AppDelegate.swift 檔案內容取代為:Replace the contents of the autogenerated AppDelegate.swift file by:
    import UIKit
    
    @UIApplicationMain
    class AppDelegate: UIResponder, UIApplicationDelegate {
        
        var window: UIWindow?
        
        func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
            window = UIWindow(frame: UIScreen.main.bounds)
            
            let homeViewController = ViewController()
            homeViewController.view.backgroundColor = UIColor.white
            window!.rootViewController = homeViewController
            
            window?.makeKeyAndVisible()
            return true
        }
    }
    
  4. 將自動產生的 ViewController.swift 檔案內容取代為:Replace the contents of the autogenerated ViewController.swift file by:
    import UIKit
    
    class ViewController: UIViewController {
        var label: UILabel!
        var fromMicButton: UIButton!
        
        var sub: String!
        var region: String!
        
        override func viewDidLoad() {
            super.viewDidLoad()
            
            // load subscription information
            sub = "YourSubscriptionKey"
            region = "YourServiceRegion"
            
            label = UILabel(frame: CGRect(x: 100, y: 100, width: 200, height: 200))
            label.textColor = UIColor.black
            label.lineBreakMode = .byWordWrapping
            label.numberOfLines = 0
            
            label.text = "Recognition Result"
            
            fromMicButton = UIButton(frame: CGRect(x: 100, y: 400, width: 200, height: 50))
            fromMicButton.setTitle("Recognize", for: .normal)
            fromMicButton.addTarget(self, action:#selector(self.fromMicButtonClicked), for: .touchUpInside)
            fromMicButton.setTitleColor(UIColor.black, for: .normal)
            
            self.view.addSubview(label)
            self.view.addSubview(fromMicButton)
        }
        
        
        @objc func fromMicButtonClicked() {
            DispatchQueue.global(qos: .userInitiated).async {
                self.recognizeFromMic()
            }
        }
        
        func recognizeFromMic() {
            var speechConfig: SPXSpeechConfiguration?
            do {
                try speechConfig = SPXSpeechConfiguration(subscription: sub, region: region)
            } catch {
                print("error \(error) happened")
                speechConfig = nil
            }
            speechConfig?.speechRecognitionLanguage = "en-US"
            
            let audioConfig = SPXAudioConfiguration()
            
            let reco = try! SPXSpeechRecognizer(speechConfiguration: speechConfig!, audioConfiguration: audioConfig)
            
            reco.addRecognizingEventHandler() {reco, evt in
                print("intermediate recognition result: \(evt.result.text ?? "(no result)")")
                self.updateLabel(text: evt.result.text, color: .gray)
            }
            
            updateLabel(text: "Listening ...", color: .gray)
            print("Listening...")
            
            let result = try! reco.recognizeOnce()
            print("recognition result: \(result.text ?? "(no result)")")
            updateLabel(text: result.text, color: .black)
        }
        
        func updateLabel(text: String?, color: UIColor) {
            DispatchQueue.main.async {
                self.label.text = text
                self.label.textColor = color
            }
        }
    }
    
  5. ViewController.swift 中,將字串 YourSubscriptionKey 取代為您的訂用帳戶金鑰。In ViewController.swift, replace the string YourSubscriptionKey with your subscription key.
  6. 以與您的訂用帳戶 (例如,免費試用訂用帳戶的 westus) 相關聯的區域取代 YourServiceRegion 字串。Replace the string YourServiceRegion with the region associated with your subscription (for example, westus for the free trial subscription).

將 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 :ios, '9.3'
        pod 'MicrosoftCognitiveServicesSpeech-iOS', '~> 1.6'
        use_frameworks!
    end
    
  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.

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

  1. 在 XCode 中開啟 helloworld.xcworkspace 工作區。Open the helloworld.xcworkspace workspace in Xcode.
  2. 顯示偵錯輸出 ([檢視] > [偵錯區域] > [啟動主控台] )。Make the debug output visible (View > Debug Area > Activate Console).
  3. 在 [產品] > [目的地] 功能表中的清單中,選擇 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.
  4. 建置範例程式碼,然後從功能表中選取 [產品] > [執行] 或按一下 [播放] 按鈕,在 iOS 模擬器中加以執行。Build and run the example code in the iOS simulator by selecting Product > Run from the menu or clicking the Play button.
  5. 按一下應用程式中的 [辨識] 按鈕,並說出幾個字後,應該會在螢幕的下半部看到說出的文字。After you click the "Recognize" button in the app and say a few words, you should see the text you have spoken on the lower part of the screen.

後續步驟Next steps