快速入门:在 iOS 上使用语音 SDK 通过 Swift 识别语音Quickstart: Recognize speech in Swift on iOS by using the Speech SDK

针对语音合成也提供了快速入门。Quickstarts are also available for speech synthesis.

本文介绍如何使用 Azure 认知服务语音 SDK 在 Swift 中创建一个 iOS 应用,以便将通过麦克风录制的语音转录为文本。In this article, you learn how to create an iOS app in Swift by using the Azure Cognitive Services Speech SDK to transcribe speech recorded from a microphone to text.

先决条件Prerequisites

准备工作:Before you get started, you'll need:

获取用于 iOS 的语音 SDKGet the Speech SDK for iOS

Important

下载任何 Azure 认知服务语音 SDK,即表示你已确认接受其许可条款。By downloading any of the Azure Cognitive Services Speech SDKs, you acknowledge its license. 有关详细信息,请参阅:For more information, see:

Important

需要语音 SDK 版本 1.11.0 或更高版本。Speech SDK version 1.11.0 or later is required.

本教程不适用于 1.6.0 之前的 SDK 版本。This tutorial won't work with a 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/iosbinary 下载,然后手动与它建立链接。It can be used in Xcode projects as a CocoaPod or downloaded from https://aka.ms/csspeech/iosbinary and linked manually. 本文使用 CocoaPod。This article uses a CocoaPod.

创建 Xcode 项目Create an Xcode project

启动 Xcode,然后通过选择“文件” > “新建” > “项目”来启动新项目。 Start Xcode, and start a new project by selecting File > New > Project. 在模板选择对话框中,选择“iOS 单一视图应用”模板。In the template selection dialog box, select the iOS Single View App template.

在随后的对话框中,进行以下选择。In the dialog boxes that follow, make the following selections.

  1. 在“项目选项”对话框中,执行以下操作:In the Project Options dialog box:

    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, use a name like testorg. 若要对应用进行签名,需要适当的预配配置文件。To sign the app, you need a proper provisioning profile. 有关详细信息,请参阅 Apple 开发人员站点For more information, see the Apple developer site.
    3. 确保选择 Swift 作为项目的语言。Make sure Swift is chosen as the language for the project.
    4. 禁用使用情节提要和创建基于文档的应用程序的复选框。Disable the check boxes to use storyboards and to create a document-based application. 以编程方式创建示例应用的简单 UI。The simple UI for the sample app is created programmatically.
    5. 清除所有用于测试和核心数据的复选框。Clear all the check boxes for tests and core data.
  2. 选择项目目录:Select a project directory:

    1. 选择用于放置该项目的目录。Choose a directory to put the project in. 此步骤会在所选目录中创建一个 helloworld 目录,其中包含 Xcode 项目的所有文件。This step 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. 选择概述中的文件,然后添加“隐私 - 麦克风使用说明”键,其值类似于“语音识别所需的麦克风”。Select 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 中的设置

  4. 关闭 Xcode 项目。Close the Xcode project. 稍后在设置 CocoaPods 后,将使用该项目的另一个实例。You use a different instance of it later after you set up the CocoaPods.

添加示例代码Add the sample code

  1. 将名为 MicrosoftCognitiveServicesSpeech-Bridging-Header.h 的新头文件放置到 helloworld 项目内的 helloworld 目录中。Place a new header file with the name MicrosoftCognitiveServicesSpeech-Bridging-Header.h into the helloworld directory inside the helloworld project. 将以下代码粘贴到该文件中: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.

    标头属性

  3. 将自动生成的 AppDelegate.swift 文件的内容替换为以下代码:Replace the contents of the autogenerated AppDelegate.swift file with the following code:

    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 with the following code:

    import UIKit
    
    class ViewController: UIViewController {
        var label: UILabel!
        var fromMicButton: UIButton!
    
        var host: String!
        var sub: String!
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            // load subscription information
    
            host = "wss://YourServiceRegion.stt.speech.azure.cn/"
            sub = "YourSubscriptionKey"
    
            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(host: host, subscription: sub)
            } 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. 将字符串 YourServiceRegion 替换为与订阅关联的区域Replace the string YourServiceRegion with the region associated with your subscription. 例如,将 chinaeast2 用于试用订阅。For example, use chinaeast2 for the trial subscription.

安装用作 CocoaPod 的 SDKInstall the SDK as a CocoaPod

  1. 根据安装说明中所述,安装 CocoaPod 依赖项管理器。Install the CocoaPod dependency manager as described in its installation instructions.

  2. 转到示例应用所在的目录,即 helloworld。Go to the directory of your sample app, which is 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 installGo to the helloworld directory in a terminal, and run the command pod install. 此命令生成一个 helloworld.xcworkspace Xcode 工作区,其中包含示例应用以及用作依赖项的语音 SDK。This command generates a helloworld.xcworkspace Xcode workspace that contains both the sample app and the Speech SDK as a dependency. 在以下步骤中使用此工作区。This workspace is used in the following steps.

生成并运行示例Build and run the sample

  1. 在 Xcode 中打开 helloworld.xcworkspace 工作区。Open the workspace helloworld.xcworkspace in Xcode.
  2. 使调试输出可见,方法是:选择“视图” > “调试区域” > “激活控制台”。Make the debug output visible by selecting 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. 也可选择“播放”按钮。You also can select the Play button.
  5. 选择应用中的“识别”按钮并讲几句话后,应会在屏幕下方看到讲出的文本。After you select 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