AVSpeechSynthesizer是属于AVFounztion框架里面的AVFAudio子框架的一个类, 主要的功能是把文本内容通过系统语音读取出来, 支持的多国语言, 中文支持普通话和粤语。
主要的类有:
AVSpeechSynthesizer
: 可以理解为说读器,为主要类
AVSpeechSynthesisVoice
: 读取文本要用什么语言说出来(辅助类)
AVSpeechUtterance
: 这个可以理解为每一段话的信息设置(辅助类)
AVSpeechSynthesisVoice
这个主要是为每一段文本设置的语言, 例如: 一篇文章的不同句子或段落我可以用不同的语言读出来, 中文的就可以设置为普通话或者粤语。
方法
+ (NSArray<AVSpeechSynthesisVoice *> *)speechVoices; //系统支持读取文本播放的语言
+ (NSString *)currentLanguageCode;//获取当前手机的语言
//根据上面两个方法获取到支持的语言来创建一个语言对象
+ (nullable AVSpeechSynthesisVoice *)voiceWithLanguage:(nullable NSString *)languageCode;
+ (nullable AVSpeechSynthesisVoice *)voiceWithIdentifier:(NSString *)identifier NS_AVAILABLE_IOS(9_0);
//这些都是只读属性
@property(nonatomic, readonly) NSString *language;
@property(nonatomic, readonly) NSString *identifier NS_AVAILABLE_IOS(9_0);
@property(nonatomic, readonly) NSString *name NS_AVAILABLE_IOS(9_0);
@property(nonatomic, readonly) AVSpeechSynthesisVoiceQuality quality NS_AVAILABLE_IOS(9_0);
AVSpeechUtterance
每一段的播放文本信息设置,
/***需要读的文字数组***/
-(NSArray *)defaultStringNeedToSpeechArray{
_stringNeedToSpeechArray = @[@"你这个傻逼",
@"ZFPlayer是对AVPlayer的封装,有人会问它支持什么格式的视频播放,问这个问题的可以自行搜索AVPlayer支持的格式",
@"Are you excited about the book?",
@"Very! I have always felt so misunderstood.",
@"What's your favorite feature?",
@"Oh, they're all my babies. I couldn't possibly choose.",
@"It was great to speak with you!",
@"The pleasure was all mine! Have fun!",
@"Hello AV Foundation. How are you?",
@"I'm well! Thanks for asking.",
@"Are you excited about the book?",
@"Very! I have always felt so misunderstood.",
@"What's your favorite feature?",
@"Oh, they're all my babies. I couldn't possibly choose.",
@"It was great to speak with you!",
@"The pleasure was all mine! Have fun!"];;
return _stringNeedToSpeechArray;
}
-(void)play {
for (NSUInteger i = 0; i < self.stringNeedToSpeechArray.count; i++) {
AVSpeechUtterance *utterance = [AVSpeechUtterance speechUtteranceWithString:self.stringNeedToSpeechArray[i]];
//zh-CN 普通话, zh-HK粤语 [AVSpeechSynthesisVoice currentLanguageCode] 根据当前的语言设置来读
utterance.voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"zh-HK"];
utterance.volume = 1.0; //音量 0-1;
utterance.rate = 0.5f; //阅读的速率 0-1
utterance.pitchMultiplier = 0.8; ////音调, 默认为1.0 , 取值范围为0.5 - 2.0
utterance.postUtteranceDelay = 0.1f; //在读下一句的时候, 延时0.1s
[self.speechSynthesizer speakUtterance:utterance];//开始播放
}
}
AVSpeechSynthesizer
这个类主要控制文本的读取, 暂停, 停止
-(void)puse {
if ([self.speechSynthesizer pauseSpeakingAtBoundary:AVSpeechBoundaryImmediate]) {
NSLog(@"成功暂停");
}else {
NSLog(@"暂停失败");
}
}
-(void)stop {
//停止读音,传入的参数 立刻停止还是读完一个单词停止
if( [self.speechSynthesizer stopSpeakingAtBoundary:AVSpeechBoundaryWord]){
NSLog(@"成功停止");
}else {
NSLog(@"停止失败");
}
}
-(void)contiune {
if( [self.speechSynthesizer continueSpeaking]){
NSLog(@"成功播放");
}else {
NSLog(@"播放失败");
}
}
它的代理也比较简单
-(void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didStartSpeechUtterance:(AVSpeechUtterance *)utterance {
//每一个utterance对象读取之前回调
NSLog(@"%s", __func__);
}
-(void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didPauseSpeechUtterance:(AVSpeechUtterance *)utterance {
NSLog(@"%s", __func__);
}
-(void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didCancelSpeechUtterance:(AVSpeechUtterance *)utterance {
NSLog(@"%s", __func__);
}
-(void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didContinueSpeechUtterance:(AVSpeechUtterance *)utterance {
NSLog(@"%s", __func__);
}
-(void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didFinishSpeechUtterance:(AVSpeechUtterance *)utterance {
//每一个utterance对象读取完之后回调
NSLog(@"%s", __func__);
}
-(void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer willSpeakRangeOfSpeechString:(NSRange)characterRange utterance:(AVSpeechUtterance *)utterance {
//根据读取的范围, 或者读取的文字内容做自定义操作
NSLog(@"%@---", synthesizer.outputChannels);
}