您的位置:首页 > 移动开发 > IOS开发

iOS 系统自带的文本转语音

2015-10-22 10:03 579 查看
第一步:引入头文件

#import <AVFoundation/AVSpeechSynthesis.h>


第二步:创建speechSynthesizer合成器实例

AVSpeechSynthesizer *speechSynthesizer = [[AVSpeechSynthesizer alloc]init];


第三步:根据你要转换的文本,创建一个属于你的专属“代言人”

AVSpeechUtterance *utteranceNuoYi = [[AVSpeechUtterance alloc]initWithString:@"Attraper des étoiles "];

AVSpeechUtterance *utteranceLiuYe = [[AVSpeechUtterance alloc]initWithString:@"Noé, chéri "];


第四步:根据你对代言人的要求,可以更改代言人的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;


第五步:把你的代言人 添加到你的整个流程上,之后就可以发言了。

根据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];


以上就已经实现了文本转语音的功能。

带有详细解释的思维导图总结

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: