当前位置: 首页 > 工具软件 > iOS7 Sampler > 使用案例 >

iOS7新特性-AVSpeechSynthesizer详解

吴哲
2023-12-01
前言
本文由DevDiv版主 @jas  原创翻译,转载请注明出处!
原文地址:
http://www.shinobicontrols.com/b ... vspeechsynthesizer/

介绍
从IOS5开始,IOS系统已经在siri上集成了语音合成的功能,但是是私有API。但是在IOS7,新增了一个简单的API----AVSpeechSynthesizer来做这件事情。

嗓音
IOS7包含了一组可以用来合成的声音的嗓音,你可以自定义多种嗓音来合成。 AVSpeechSynthesisVoice:speechVoices返回当前可用的嗓音数组:

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" )

用下面的方法来创建一个特殊的嗓音:
AVSpeechSynthesisVoice *voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"en-US"];

如果嗓音不能被识别,返回值为nil。

话语
话语代表一段讲话,可以传递到语音合成器来生成一段语音流。话语可以用待发音的字符串来创建:
AVSpeechUtterance *utterance = [AVSpeechUtterance speechUtteranceWithString:@"Hello world!"];

我们可以设置它的嗓音属性voice:
utterence.voice = voice;

还有其他各种属性,包括:rate,volume和pitchMultiplier。例如,稍微放慢语速:
utterance.rate *= 0.7;

当话语创建完成,可以把它放到语音合成器中。语音合成器会生成音频:
AVSpeechSynthesizer *speechSynthesizer = [[AVSpeechSynthesizer alloc] init];
[speechSynthesizer speakUtterance:utterance];

话语会被合成器放到队列中,所以不用等待上一个结束,可以连续传送。但是,如果你尝试传送一个已经在队列中的话语,系统会抛出异常。

实现
本文的例子是一个关于多语言问候语的应用。这充分体现了语音合成的通用性。
注意:定义话语的字符串都是罗马字符,例如中文的'Ni hao'。示例项目定义了一个类,可以生成一组嗓音对应的话语。
项目有一个选择器供用户选择语言,然后按按钮可以听到对应的生成的语音。

总结

在IOS7,语音合成器非常简单,而且包含很多语言。合理的使用,可以提高应用的访问性,而不用时刻用你的手和眼来操作应用。

  #import <AVFoundation/AVSpeechSynthesis.h>

//    传递到语音合成器来生成一段语音流。话语可以用待发音的字符串来创建

    AVSpeechUtterance *utterance = [[AVSpeechUtterancealloc]initWithString:@"你好吗,很高兴见到你"];//需要转换的文本

    AVSpeechSynthesisVoice *voice = [AVSpeechSynthesisVoicevoiceWithLanguage:@"zh-CN"];

    //我们可以设置它的嗓音属性voice

    utterance.voice = voice;

    

    //还有其他各种属性,包括:ratevolumepitchMultiplier。例如,稍微放慢语速:

    utterance.rate *=0.5;

    //当话语创建完成,可以把它放到语音合成器中。语音合成器会生成音频:

    AVSpeechSynthesizer *av = [[AVSpeechSynthesizeralloc]init];

    [avspeakUtterance:utterance];



 类似资料: