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);
}