iOS中的AVSpeechSynthesizer可以很轻松的实现实现文本到语音的功能,基本代码如下:
self.speechSynthesizer = [[AVSpeechSynthesizer alloc] init];
AVSpeechUtterance *utterance = [AVSpeechUtterance speechUtteranceWithString:@"FlyElephant"];
AVSpeechSynthesisVoice *voiceType = [AVSpeechSynthesisVoice voiceWithLanguage:@"en-US"];
utterance.voice = voiceType;
//设置语速
utterance.rate *= 0.5;
//设置音量
utterance.volume = 0.6;
[self.speechSynthesizer speakUtterance:utterance];
AVSpeechUtterance可以设置对应的语言,如果设置的语言不能识别文本不能生成语音播放,苹果支持的语言如下:
- Arabic (ar-SA)
- Chinese (zh-CN, zh-HK, zh-TW)
- Czech (cs-CZ)
- Danish (da-DK)
- Dutch (nl-BE, nl-NL)
- English (en-AU, en-GB, en-IE, en-US, en-ZA)
- Finnish (fi-FI)
- French (fr-CA, fr-FR)
- German (de-DE)
- Greek (el-GR)
- Hebrew (he-IL)
- Hindi (hi-IN)
- Hungarian (hu-HU)
- Indonesian (id-ID)
- Italian (it-IT)
- Japanese (ja-JP)
- Korean (ko-KR)
- Norwegian (no-NO)
- Polish (pl-PL)
- Portuguese (pt-BR, pt-PT)
- Romanian (ro-RO)
- Russian (ru-RU)
- Slovak (sk-SK)
- Spanish (es-ES, es-MX)
- Swedish (sv-SE)
- Thai (th-TH)
- Turkish (tr-TR)
以上就是苹果支持的语言编码,当然你也可以通过speechVoices遍历对应的语言编码:
NSArray *voice = [AVSpeechSynthesisVoice speechVoices];
for (AVSpeechSynthesisVoice *voiceModel in voice) {
NSLog(@"FlyElephant-%@",voiceModel);
}