RQVoiceManager.m 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. //
  2. // RQVoiceManager.m
  3. // YJZS
  4. //
  5. // Created by 张嵘 on 2022/2/25.
  6. //
  7. #import "RQVoiceManager.h"
  8. #import <AVFoundation/AVFoundation.h>
  9. static NSString *is_voiceSwitch = nil;
  10. /// 语音开关
  11. static NSString * const RQVoiceSwitchKey = @"RQApplicationAppStoreFormalSettingKey";
  12. static RQVoiceManager *manger = nil;
  13. static dispatch_once_t onceToken;
  14. @interface RQVoiceManager () <AVSpeechSynthesizerDelegate>
  15. @property (nonatomic, readwrite, strong) AVSpeechSynthesizer *avSpeechSynthesizer;
  16. @property (nonatomic, readwrite, strong) AVSpeechUtterance *avSpeechUtterance;
  17. @property (nonatomic, readwrite, strong) AVSpeechSynthesisVoice *avSpeechSynthesisVoice;
  18. @property (nonatomic, readwrite, assign) BOOL voiceSwitch;
  19. @end
  20. @implementation RQVoiceManager
  21. #pragma mark - SystemMethod
  22. + (RQVoiceManager *)sharedManager {
  23. dispatch_once(&onceToken, ^{
  24. manger = [[self alloc] init];
  25. });
  26. return manger;
  27. }
  28. - (instancetype)init {
  29. self = [super init];
  30. if (self) {
  31. }
  32. return self;
  33. }
  34. - (void)playString:(NSString *)str {
  35. _avSpeechUtterance = [[AVSpeechUtterance alloc] initWithString:str];
  36. //设置语速,语速介于AVSpeechUtteranceMaximumSpeechRate和AVSpeechUtteranceMinimumSpeechRate之间
  37. //AVSpeechUtteranceMaximumSpeechRate
  38. //AVSpeechUtteranceMinimumSpeechRate
  39. //AVSpeechUtteranceDefaultSpeechRate
  40. _avSpeechUtterance.rate = 0.5;
  41. //设置音高,[0.5 - 2] 默认 = 1
  42. //AVSpeechUtteranceMaximumSpeechRate
  43. //AVSpeechUtteranceMinimumSpeechRate
  44. //AVSpeechUtteranceDefaultSpeechRate
  45. _avSpeechUtterance.pitchMultiplier = 1;
  46. //设置音量,[0-1] 默认 = 1
  47. _avSpeechUtterance.volume = 1;
  48. //读一段前的停顿时间
  49. _avSpeechUtterance.preUtteranceDelay = 1;
  50. //读完一段后的停顿时间
  51. _avSpeechUtterance.postUtteranceDelay = 1;
  52. //设置声音,是AVSpeechSynthesisVoice对象
  53. _avSpeechUtterance.voice = self.avSpeechSynthesisVoice;
  54. //开始朗读
  55. if (self.voiceSwitch) {
  56. [self.avSpeechSynthesizer speakUtterance:self.avSpeechUtterance];
  57. }
  58. }
  59. #pragma mark - AVSpeechSynthesizerDelegate
  60. //已经开始
  61. - (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didStartSpeechUtterance:(AVSpeechUtterance *)utterance{
  62. }
  63. //已经说完
  64. - (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didFinishSpeechUtterance:(AVSpeechUtterance *)utterance{
  65. //如果朗读要循环朗读,可以在这里再次调用朗读方法
  66. //[_avSpeaker speakUtterance:utterance];
  67. }
  68. //已经暂停
  69. - (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didPauseSpeechUtterance:(AVSpeechUtterance *)utterance{
  70. }
  71. //已经继续说话
  72. - (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didContinueSpeechUtterance:(AVSpeechUtterance *)utterance{
  73. }
  74. //已经取消说话
  75. - (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didCancelSpeechUtterance:(AVSpeechUtterance *)utterance{
  76. }
  77. //将要说某段话
  78. - (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer willSpeakRangeOfSpeechString:(NSRange)characterRange utterance:(AVSpeechUtterance *)utterance{
  79. }
  80. #pragma mark - LazyLoad
  81. - (AVSpeechSynthesizer *)avSpeechSynthesizer {
  82. // 初始化语音合成器
  83. if (!_avSpeechSynthesizer) {
  84. _avSpeechSynthesizer = [[AVSpeechSynthesizer alloc] init];
  85. _avSpeechSynthesizer.delegate = self;
  86. }
  87. return _avSpeechSynthesizer;
  88. }
  89. - (AVSpeechSynthesisVoice *)avSpeechSynthesisVoice {
  90. //AVSpeechSynthesisVoice定义了一系列的声音, 主要是不同的语言和地区.
  91. //voiceWithLanguage: 根据制定的语言, 获得一个声音.
  92. //speechVoices: 获得当前设备支持的声音
  93. //currentLanguageCode: 获得当前声音的语言字符串, 比如”ZH-cn”
  94. //language: 获得当前的语言
  95. //通过特定的语言获得声音
  96. if (!_avSpeechSynthesisVoice) {
  97. //通过voicce标示获得声音
  98. //_avSpeechSynthesisVoice = [AVSpeechSynthesisVoice voiceWithIdentifier:AVSpeechSynthesisVoiceIdentifierAlex];
  99. _avSpeechSynthesisVoice = [AVSpeechSynthesisVoice voiceWithLanguage:@"zh-CN"];
  100. }
  101. return _avSpeechSynthesisVoice;
  102. }
  103. - (void)setVoiceSwitch:(BOOL)voiceSwitch {
  104. is_voiceSwitch = voiceSwitch? @"1" : @"0";
  105. [[NSUserDefaults standardUserDefaults] setObject:is_voiceSwitch forKey:RQVoiceSwitchKey];
  106. [[NSUserDefaults standardUserDefaults] synchronize];
  107. }
  108. - (BOOL)voiceSwitch {
  109. is_voiceSwitch = [[NSUserDefaults standardUserDefaults] objectForKey:RQVoiceSwitchKey];
  110. is_voiceSwitch = [is_voiceSwitch rq_stringValueExtension];
  111. return (is_voiceSwitch.integerValue == 1);
  112. }
  113. @end