最近零零散散的看了点关于AVFoundation
的资料,觉得很不系统,决定系统的学习一下。官方文档,请点击这里查看。
- 通过下面的图,我们可以知道
AVFoundation
的整体结构:
使用AVFoundation
来播报文字
1、导入框架:
#import <AVFoundation/AVFoundation.h>
2、初始化AVSpeechSynthesizer
@property (strong, nonatomic) AVSpeechSynthesizer *synthesizer;
_synthesizer = [[AVSpeechSynthesizer alloc] init];
3、初始化AVSpeechUtterance
播报文字:
- 我们在播放之前,还需要配置voice,这里我配置的是中文:
_synthesizer = [[AVSpeechSynthesizer alloc] init];
NSString *str = @"日照香炉生紫烟";
AVSpeechUtterance *utterance =
[[AVSpeechUtterance alloc] initWithString:str];
AVSpeechSynthesisVoice *voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"zh-CN"];
utterance.voice = voice;
utterance.rate = 0.5f;
utterance.pitchMultiplier = 0.8f;
utterance.postUtteranceDelay = 0.1f;
[self.synthesizer speakUtterance:utterance];