天天品尝iOS7甜点 :: Day 4 :: AVSpeechSynthesizer

这篇文章是天天品尝iOS7甜点系列的一部分,你可以查看完整的系列目录:天天品尝iOS7甜点


介绍

自动iOS5发布之后,语音合成就是siri的一部分了,但是它没有暴露出可用功能的公共API。iOS7改变了这些,开放了一个简单的API - AVSpeechSynthesizer

完成的代码已经在github可供下载使用:github.com/ShinobiControls/iOS7-day-by-day

Voices - 语音

iOS7中包含了不同的种类的语音可以用来进行语音合成,你自己指定不同合成的语言。AVSpeechSynthesisVoce:speechVoices返回一组可用的语音集合:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
2013-07-12 10:49:26.929 GreetingSpeaker[31267:70b] (
"[AVSpeechSynthesisVoice 0x978a0b0] Language: th-TH",
"[AVSpeechSynthesisVoice 0x977a450] Language: pt-BR",
"[AVSpeechSynthesisVoice 0x977a480] Language: sk-SK",
"[AVSpeechSynthesisVoice 0x978ad50] Language: fr-CA",
"[AVSpeechSynthesisVoice 0x978ada0] Language: ro-RO",
"[AVSpeechSynthesisVoice 0x97823f0] Language: no-NO",
"[AVSpeechSynthesisVoice 0x978e7b0] Language: fi-FI",
"[AVSpeechSynthesisVoice 0x978af50] Language: pl-PL",
"[AVSpeechSynthesisVoice 0x978afa0] Language: de-DE",
"[AVSpeechSynthesisVoice 0x978e390] Language: nl-NL",
"[AVSpeechSynthesisVoice 0x978b030] Language: id-ID",
"[AVSpeechSynthesisVoice 0x978b080] Language: tr-TR",
"[AVSpeechSynthesisVoice 0x978b0d0] Language: it-IT",
"[AVSpeechSynthesisVoice 0x978b120] Language: pt-PT",
"[AVSpeechSynthesisVoice 0x978b170] Language: fr-FR",
"[AVSpeechSynthesisVoice 0x978b1c0] Language: ru-RU",
"[AVSpeechSynthesisVoice 0x978b210] Language: es-MX",
"[AVSpeechSynthesisVoice 0x978b2d0] Language: zh-HK",
"[AVSpeechSynthesisVoice 0x978b320] Language: sv-SE",
"[AVSpeechSynthesisVoice 0x978b010] Language: hu-HU",
"[AVSpeechSynthesisVoice 0x978b440] Language: zh-TW",
"[AVSpeechSynthesisVoice 0x978b490] Language: es-ES",
"[AVSpeechSynthesisVoice 0x978b4e0] Language: zh-CN",
"[AVSpeechSynthesisVoice 0x978b530] Language: nl-BE",
"[AVSpeechSynthesisVoice 0x978b580] Language: en-GB",
"[AVSpeechSynthesisVoice 0x978b5d0] Language: ar-SA",
"[AVSpeechSynthesisVoice 0x978b620] Language: ko-KR",
"[AVSpeechSynthesisVoice 0x978b670] Language: cs-CZ",
"[AVSpeechSynthesisVoice 0x978b6c0] Language: en-ZA",
"[AVSpeechSynthesisVoice 0x978aed0] Language: en-AU",
"[AVSpeechSynthesisVoice 0x978af20] Language: da-DK",
"[AVSpeechSynthesisVoice 0x978b810] Language: en-US",
"[AVSpeechSynthesisVoice 0x978b860] Language: en-IE",
"[AVSpeechSynthesisVoice 0x978b8b0] Language: hi-IN",
"[AVSpeechSynthesisVoice 0x978b900] Language: el-GR",
"[AVSpeechSynthesisVoice 0x978b950] Language: ja-JP"
)

你可以用下面的方法指定你要设置的语音:

1
AVSpeechSynthesisVoce *voice = [AVSpeechSynthesisVocie voiceWithLanguage:@"en-US"];

如果自己设置的语言无法识别,则voice就会赋值为nil。

Utterances - 演说

演说代表了一系列的语音合成的集合。一个字符串可以用来创建一个演说,然后可以通过语音合成器进行发声:

1
AVSpeechUtterance *utterance = [AVSpeechUtterance sppeechUtteranceWithString:@"Hello world!"];

我们可以指定发声源:

1
utterance.voice = voice;

有需要的属性可以对演说(utterance)进行设置,包含rate, volume和pitchMultiplier.例如,我们可以让语速放慢一点:

1
utterance.rate *= 0.7;

创建了之后就可以用语音合成器生成音频:

1
2
AVSpeechSynthesizer *speechSynthesizer = [[AVSpeechSynthesizer alloc] init];
[speechSynthesizer speakUtterance:utterance];

由于通过演说生成是队列式的,所以你可以继续进行播放演说下去并不需要等待生成结束。

Implementation - 实现

本文携带的应用是一个多语言问候应用,这说明在iOS7中语音合成是通用性的。

值得重点注意的就是,设置的字符串要选择是罗马字母, 例如中文的’Ni hao’项目中定义了使用多少不同语言的演说。

项目中还提供了可以选择的语言,然后选择点击进行问候。

Conclusion - 总结

在iOS7中,语音合成变得十分的容易,具有多种的不同语言。从而可以更加的为那些视力不好的人群提供更高的用户体验。

本文翻译自:iOS7 Day-by-Day :: Day 4 :: AVSpeechSynthesizer

文章目录
  1. 1. 介绍
  2. 2. Voices - 语音
  3. 3. Utterances - 演说
  4. 4. Implementation - 实现
  5. 5. Conclusion - 总结
,