---
title: "iOS TTS文字转语音"
author: "折句号"
date: "2016-05-18T15:10:59Z"
tags: []
categories: ["移动开发"]
slug: "ios-tts"
draft: false
lastmod: "2016-05-18T15:10:59Z"
id: 2299052
url: "https://404530969.iteye.com/blog/2299052"
---

iOS自带的文字转语音是iOS7以后才有的，需要AVFoundation 库。

AVSpeechSynthesizer: 语音合成器, 可以假想成一个可以说话的人, 是最主要的接口

AVSpeechSynthesisVoice: 可以假想成人的声音

AVSpeechUtterance: 可以假想成要说的一段话

 

 语言种类 : Language

         ar-SA  沙特阿拉伯（阿拉伯文）

         en-ZA, 南非（英文）

         nl-BE, 比利时（荷兰文）

         en-AU, 澳大利亚（英文）

         th-TH, 泰国（泰文）

         de-DE, 德国（德文）

         en-US, 美国（英文）

         pt-BR, 巴西（葡萄牙文）

         pl-PL, 波兰（波兰文）

         en-IE, 爱尔兰（英文）

         el-GR, 希腊（希腊文）

         id-ID, 印度尼西亚（印度尼西亚文）

         sv-SE, 瑞典（瑞典文）

         tr-TR, 土耳其（土耳其文）

         pt-PT, 葡萄牙（葡萄牙文）

         ja-JP, 日本（日文）

         ko-KR, 南朝鲜（朝鲜文）

         hu-HU, 匈牙利（匈牙利文）

         cs-CZ, 捷克共和国（捷克文）

         da-DK, 丹麦（丹麦文）

         es-MX, 墨西哥（西班牙文）

         fr-CA, 加拿大（法文）

         nl-NL, 荷兰（荷兰文）

         fi-FI, 芬兰（芬兰文）

         es-ES, 西班牙（西班牙文）

         it-IT, 意大利（意大利文）

         he-IL, 以色列（希伯莱文，阿拉伯文）

         no-NO, 挪威（挪威文）

         ro-RO, 罗马尼亚（罗马尼亚文）

         zh-HK, 香港（中文）

         zh-TW, 台湾（中文）

         sk-SK, 斯洛伐克（斯洛伐克文）

         zh-CN, 中国（中文）

         ru-RU, 俄罗斯（俄文）

         en-GB, 英国（英文）

         fr-FR, 法国（法文）

         hi-IN  印度（印度文）

```
NSString *language = @"en-US";
    AVSpeechSynthesizer  *speechSynthesizer = [[AVSpeechSynthesizer alloc]init];
    AVSpeechSynthesisVoice *voice = [AVSpeechSynthesisVoice voiceWithLanguage:language];
    AVSpeechUtterance  *utterance =[AVSpeechUtterance   speechUtteranceWithString:text]; //要读的文字
    utterance.voice = voice;
    utterance.rate = 0.5;//语速0~1
    utterance.pitchMultiplier = 0.5;//声调0.5~2
    utterance.volume =1.0;
    [speechSynthesizer speakUtterance:utterance];

```

 

还有代理提供的一些方法。

```
@protocol AVSpeechSynthesizerDelegate <NSObject>

@optional
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didStartSpeechUtterance:(AVSpeechUtterance *)utterance;
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didFinishSpeechUtterance:(AVSpeechUtterance *)utterance;
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didPauseSpeechUtterance:(AVSpeechUtterance *)utterance;
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didContinueSpeechUtterance:(AVSpeechUtterance *)utterance;
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didCancelSpeechUtterance:(AVSpeechUtterance *)utterance;

- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer willSpeakRangeOfSpeechString:(NSRange)characterRange utterance:(AVSpeechUtterance *)utterance;
@end
```

 

iOS 文本转语音比较简单，值得注意的一点是如果想要单独读一串英文字母比如dunnage，需要在每个字母之间加字符来分开，“\_”会隔开字母而且不会读出来，“，”会有明显的停顿，但是在开头加字符会被读出来。一些字母可能会被识别成单词，比如qa，不会读q a ，这时只要转换成大写字母QA即可。