快速入门:在 iOS 上使用语音 SDK 通过 Objective-C 合成语音Quickstart: Synthesize speech in Objective-C on iOS using the Speech SDK

本文介绍如何使用认知服务语音 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 synthesize speech from text.

先决条件Prerequisites

在开始之前,请满足以下一系列先决条件:Before you get started, here's a list of prerequisites:

  • 语音服务的订阅密钥A subscription key for the Speech service
  • Xcode 9.4.1 或更高版本的 macOS 计算机A macOS machine with Xcode 9.4.1 or later
  • 目标设置为 iOS 9.3 版或更高版本The target set to iOS version 9.3 or later
  • 需要语音 SDK 1.11.0 或更高版本。Speech SDK version 1.11.0 or later is required.

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

重要

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

用于 iOS 的认知服务语音 SDK 目前以 Cocoa Framework 形式分发。The Cognitive Services Speech SDK for iOS is currently distributed as a Cocoa Framework. 可在 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 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 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 all checkboxes for tests and core data. 项目设置Project Settings
  2. 选择项目目录Select project directory
    1. 选择用于放置项目的主目录。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.
    2. 禁止创建适用于此示例项目的 Git 存储库。Disable the creation of a Git repo for this example project.

安装用作 CocoaPod 的 SDKInstall 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.7'
        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.

添加示例代码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 "ViewController.h"
    
    @interface AppDelegate ()
    
    @end
    
    @implementation AppDelegate
    
    
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        ViewController *controller = [[ViewController alloc] init];
        self.window.rootViewController = controller;
        [self.window makeKeyAndVisible];
    
        return YES;
    }
    
    @end
    
  3. 通过以下方式替换自动生成的 ViewController.m 文件的内容:Replace the contents of the autogenerated ViewController.m file by:

    #import "ViewController.h"
    #import <AVFoundation/AVFoundation.h>
    #import <MicrosoftCognitiveServicesSpeech/SPXSpeechApi.h>
    
    @interface ViewController () {
        NSString *inputText;
    
        NSString *speechKey;
        NSString *serviceRegion;
    }
    
    @property (strong, nonatomic) IBOutlet UITextField *inputField;
    @property (strong, nonatomic) IBOutlet UIButton *synthesisButton;
    @property (strong, nonatomic) IBOutlet UILabel *resultLabel;
    
    @property (nonatomic, strong) AVAudioPlayer *player;
    
    - (IBAction)synthesisButtonTapped:(UIButton *)sender;
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
    
        inputText = @"";
        speechHost = @"https://YourServiceRegion.tts.speech.azure.cn/";
        speechKey = @"YourSubscriptionKey";
    
        [self.view setBackgroundColor:[UIColor whiteColor]];
    
        self.inputField = [[UITextField alloc] initWithFrame:CGRectMake(50, 150, 300, 50)];
        self.inputField.placeholder = @"Input something to synthesize...";
        self.inputField.borderStyle = UITextBorderStyleRoundedRect;
        self.inputField.delegate = self;
        self.inputField.accessibilityIdentifier = @"input_text_field";
        [self.view addSubview:self.inputField];
    
        self.synthesisButton = [UIButton buttonWithType:UIButtonTypeSystem];
        [self.synthesisButton addTarget:self action:@selector(synthesisButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
        [self.synthesisButton setTitle:@"Synthesis" forState:UIControlStateNormal];
        [self.synthesisButton setFrame:CGRectMake(80, 350, 200, 50)];
        self.synthesisButton.titleLabel.font = [UIFont systemFontOfSize:36.0];
        self.synthesisButton.accessibilityIdentifier = @"synthesis_button";
        [self.view addSubview:self.synthesisButton];
    
        self.resultLabel = [[UILabel alloc] initWithFrame:CGRectMake(50.0, 400.0, 300.0, 200.0)];
        self.resultLabel.lineBreakMode = NSLineBreakByWordWrapping;
        self.resultLabel.numberOfLines = 0;
        self.resultLabel.accessibilityIdentifier = @"result_label";
        [self.view addSubview:self.resultLabel];
    }
    
    - (IBAction)synthesisButtonTapped:(UIButton *)sender {
        dispatch_async(dispatch_get_global_queue(QOS_CLASS_DEFAULT, 0), ^{
            [self synthesis];
        });
    }
    
    - (void)synthesis {
        SPXSpeechConfiguration *speechConfig = [[SPXSpeechConfiguration alloc] initWithHost:speechHost subscription:speechKey];
        [speechConfig setSpeechSynthesisOutputFormat:SPXSpeechSynthesisOutputFormat_Audio16Khz32KBitRateMonoMp3];
        SPXSpeechSynthesizer *speechSynthesizer = [[SPXSpeechSynthesizer alloc] initWithSpeechConfiguration:speechConfig audioConfiguration:nil];
    
        NSLog(@"Start synthesizing...");
        [self updateText:@"Start synthesizing..." color:UIColor.grayColor];
    
        SPXSpeechSynthesisResult *speechResult = [speechSynthesizer speakText:inputText];
    
        // Checks result.
        if (SPXResultReason_Canceled == speechResult.reason) {
            SPXSpeechSynthesisCancellationDetails *details = [[SPXSpeechSynthesisCancellationDetails alloc] initFromCanceledSynthesisResult:speechResult];
            NSLog(@"Speech synthesis was canceled: %@. Did you pass the correct key/region combination?", details.errorDetails);
            [self updateText:[NSString stringWithFormat:@"Speech synthesis was canceled: %@. Did you pass the correct key/region combination?", details.errorDetails] color:UIColor.redColor];
        } else if (SPXResultReason_SynthesizingAudioCompleted == speechResult.reason) {
            NSLog(@"Speech synthesis was completed");
            // Play audio.
            self.player = [[AVAudioPlayer alloc] initWithData:[speechResult audioData] error:nil];
            [self.player prepareToPlay];
            [self.player play];
            [self updateText:@"The synthesis is completed." color:UIColor.blueColor];
        } else {
            NSLog(@"There was an error.");
            [self updateText:@"Synthesis error" color:UIColor.redColor];
        }
    }
    
    - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {;
        inputText = [textField.text stringByReplacingCharactersInRange:range withString:string];
        return true;
    }
    
    - (void)updateText:(NSString *) resultText color:(UIColor *)color{
        dispatch_async(dispatch_get_main_queue(), ^{
            self.resultLabel.textColor = color;
            self.resultLabel.text = resultText;
        });
    }
    
    @end
    
  4. 将字符串 YourSubscriptionKey 替换为你的订阅密钥。Replace the string YourSubscriptionKey with your subscription key.

  5. 将字符串 YourServiceRegion 替换为与订阅关联的区域(例如,对于试用订阅,为 chinaeast2)。Replace the string YourServiceRegion with the region associated with your subscription (for example, chinaeast2 for the trial subscription).

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

  1. 使调试输出可见(“视图” > “调试区域” > “激活控制台”)。Make the debug output visible (View > Debug Area > Activate Console).

  2. 从“产品” > “目标”菜单中的列表中,选择 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.

  3. 在 iOS 模拟器中生成并运行示例代码,方法是在菜单中选择“产品” > “运行”,或者单击“播放”按钮。Build and run the example code in the iOS simulator by selecting Product > Run from the menu or clicking the Play button.

    模拟的 iOS 应用

  4. 在输入一些文本并单击应用中的按钮后,你应该会听到播放的合成音频。After you input some text and click the button in the app, you should hear the synthesized audio played.

后续步骤Next steps