語音合成器的技術是iOS7推出的,可以實現無網絡語音功能,支持多種語言
1. 定義一個成員變量,記錄語音合成器 AVSpeechSynthesizer
#import <AVFoundation/AVFoundation.h>
1 @interfaceViewController () 2 3 { 4 5 // 合成器 6 7 AVSpeechSynthesizer *_synthesizer; 8 9 10 11 // 實例化說話的語言,說中文、英文 12 13 AVSpeechSynthesisVoice *_voice; 14 15 }
2. 定義語音對象 AVSpeechSynthesisVoice,指定說話的語言
zh_CN 中文
en-US 英文
1 - (void)viewDidLoad 2 { 3 [super viewDidLoad]; 4 5 // 實例化說話的語言,說中文 6 _voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"en-US"]; 7 8 // 要朗誦,需要一個語音合成器 9 _synthesizer = [[AVSpeechSynthesizer alloc] init]; 10 }
3. 實例化發聲對象 AVSpeechUtterance,指定要朗讀的內容
1 // 朗誦文本框中的內容 2 // 實例化發聲的對象,及朗讀的內容 3 AVSpeechUtterance *utterance = [AVSpeechUtterance speechUtteranceWithString:_textView.text];
4.指定語音,和朗誦速度
中文朗誦速度:0.1還能夠接受
英文朗誦速度:0.3還可以
1 utterance.voice = _voice; 2 3 utterance.rate = 0.3;
5.啟動
1 [_synthesizer speakUtterance:utterance];
提示:在制作應用程序時,如果朗誦的內容有限,需要提供專業的配音音頻
如果朗誦的內容無限,使用此方法是最佳選擇!
清澈Saup