iOS猿媛圈iOS开发iOS开发技术讨论

iOS-AVSpeechSynthesizer语音合成

2016-09-11  本文已影响1570人  FlyElephant

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可以设置对应的语言,如果设置的语言不能识别文本不能生成语音播放,苹果支持的语言如下:

以上就是苹果支持的语言编码,当然你也可以通过speechVoices遍历对应的语言编码:
<pre><code>NSArray *voice = [AVSpeechSynthesisVoice speechVoices]; for (AVSpeechSynthesisVoice *voiceModel in voice) { NSLog(@"FlyElephant-%@",voiceModel); }</code></pre>

上一篇 下一篇

猜你喜欢

热点阅读