// // RQVoiceManager.m // YJZS // // Created by 张嵘 on 2022/2/25. // #import "RQVoiceManager.h" #import static NSString *is_voiceSwitch = nil; /// 语音开关 static NSString * const RQVoiceSwitchKey = @"RQApplicationAppStoreFormalSettingKey"; static RQVoiceManager *manger = nil; static dispatch_once_t onceToken; @interface RQVoiceManager () @property (nonatomic, readwrite, strong) AVSpeechSynthesizer *avSpeechSynthesizer; @property (nonatomic, readwrite, strong) AVSpeechUtterance *avSpeechUtterance; @property (nonatomic, readwrite, strong) AVSpeechSynthesisVoice *avSpeechSynthesisVoice; @property (nonatomic, readwrite, assign) BOOL voiceSwitch; @end @implementation RQVoiceManager #pragma mark - SystemMethod + (RQVoiceManager *)sharedManager { dispatch_once(&onceToken, ^{ manger = [[self alloc] init]; }); return manger; } - (instancetype)init { self = [super init]; if (self) { } return self; } - (void)playString:(NSString *)str { _avSpeechUtterance = [[AVSpeechUtterance alloc] initWithString:str]; //设置语速,语速介于AVSpeechUtteranceMaximumSpeechRate和AVSpeechUtteranceMinimumSpeechRate之间 //AVSpeechUtteranceMaximumSpeechRate //AVSpeechUtteranceMinimumSpeechRate //AVSpeechUtteranceDefaultSpeechRate _avSpeechUtterance.rate = 0.5; //设置音高,[0.5 - 2] 默认 = 1 //AVSpeechUtteranceMaximumSpeechRate //AVSpeechUtteranceMinimumSpeechRate //AVSpeechUtteranceDefaultSpeechRate _avSpeechUtterance.pitchMultiplier = 1; //设置音量,[0-1] 默认 = 1 _avSpeechUtterance.volume = 1; //读一段前的停顿时间 _avSpeechUtterance.preUtteranceDelay = 1; //读完一段后的停顿时间 _avSpeechUtterance.postUtteranceDelay = 1; //设置声音,是AVSpeechSynthesisVoice对象 _avSpeechUtterance.voice = self.avSpeechSynthesisVoice; //开始朗读 if (self.voiceSwitch) { [self.avSpeechSynthesizer speakUtterance:self.avSpeechUtterance]; } } #pragma mark - AVSpeechSynthesizerDelegate //已经开始 - (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didStartSpeechUtterance:(AVSpeechUtterance *)utterance{ } //已经说完 - (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didFinishSpeechUtterance:(AVSpeechUtterance *)utterance{ //如果朗读要循环朗读,可以在这里再次调用朗读方法 //[_avSpeaker speakUtterance:utterance]; } //已经暂停 - (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didPauseSpeechUtterance:(AVSpeechUtterance *)utterance{ } //已经继续说话 - (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didContinueSpeechUtterance:(AVSpeechUtterance *)utterance{ } //已经取消说话 - (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didCancelSpeechUtterance:(AVSpeechUtterance *)utterance{ } //将要说某段话 - (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer willSpeakRangeOfSpeechString:(NSRange)characterRange utterance:(AVSpeechUtterance *)utterance{ } #pragma mark - LazyLoad - (AVSpeechSynthesizer *)avSpeechSynthesizer { // 初始化语音合成器 if (!_avSpeechSynthesizer) { _avSpeechSynthesizer = [[AVSpeechSynthesizer alloc] init]; _avSpeechSynthesizer.delegate = self; } return _avSpeechSynthesizer; } - (AVSpeechSynthesisVoice *)avSpeechSynthesisVoice { //AVSpeechSynthesisVoice定义了一系列的声音, 主要是不同的语言和地区. //voiceWithLanguage: 根据制定的语言, 获得一个声音. //speechVoices: 获得当前设备支持的声音 //currentLanguageCode: 获得当前声音的语言字符串, 比如”ZH-cn” //language: 获得当前的语言 //通过特定的语言获得声音 if (!_avSpeechSynthesisVoice) { //通过voicce标示获得声音 //_avSpeechSynthesisVoice = [AVSpeechSynthesisVoice voiceWithIdentifier:AVSpeechSynthesisVoiceIdentifierAlex]; _avSpeechSynthesisVoice = [AVSpeechSynthesisVoice voiceWithLanguage:@"zh-CN"]; } return _avSpeechSynthesisVoice; } - (void)setVoiceSwitch:(BOOL)voiceSwitch { is_voiceSwitch = voiceSwitch? @"1" : @"0"; [[NSUserDefaults standardUserDefaults] setObject:is_voiceSwitch forKey:RQVoiceSwitchKey]; [[NSUserDefaults standardUserDefaults] synchronize]; } - (BOOL)voiceSwitch { is_voiceSwitch = [[NSUserDefaults standardUserDefaults] objectForKey:RQVoiceSwitchKey]; is_voiceSwitch = [is_voiceSwitch rq_stringValueExtension]; return (is_voiceSwitch.integerValue == 1); } @end