- 第一步:引入头文件
#import <AVFoundation/AVSpeechSynthesis.h>
- 1
- 第二步:创建speechSynthesizer合成器实例
AVSpeechSynthesizer *speechSynthesizer = [[AVSpeechSynthesizer alloc]init];
- 1
- 第三步:根据你要转换的文本,创建一个属于你的专属“代言人”
AVSpeechUtterance *utteranceNuoYi = [[AVSpeechUtterance alloc]initWithString:@"Attraper des étoiles "]; AVSpeechUtterance *utteranceLiuYe = [[AVSpeechUtterance alloc]initWithString:@"Noé, chéri "];
- 1
- 2
- 3
- 第四步:根据你对代言人的要求,可以更改代言人的voice、pitch、rate
utteranceNuoYi.voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"fr-CA"]; utteranceLiuYe.voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"fr-CA"]; //音高pitch // [0.5 - 2] Default = 1,就是声音的粗细问题。 utteranceNuoYi.pitchMultiplier = 0.7; //说话的快慢 //Values are pinned between AVSpeechUtteranceMinimumSpeechRate and AVSpeechUtteranceMaximumSpeechRate. utteranceNuoYi.rate = AVSpeechUtteranceMinimumSpeechRate; //调整音量的高低。 // [0-1] Default = 1 utteranceNuoYi.volume = 0.8;
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 第五步:把你的代言人 添加到你的整个流程上,之后就可以发言了。
- 根据API:If the synthesizer is speaking, utterances are added to a queue and spoken in the order they are received.说明你的代言人是可以有多个,并且你是以一种队列(FIFO)的方式管理他们的。
[speechSynthesizer speakUtterance:utteranceNuoYi]; [speechSynthesizer speakUtterance:utteranceLiuYe];
- 1
- 2
- 3
以上就已经实现了文本转语音的功能。
avspeech支持的语言种类包括:
"[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"