BDVRSettings.m 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. //
  2. // BDVRSettings.m
  3. // BDVRClientDemo
  4. //
  5. // Created by baidu on 16/3/14.
  6. // Copyright © 2016年 baidu. All rights reserved.
  7. //
  8. #import "BDVRSettings.h"
  9. #import "BDSEventManager.h"
  10. NSString *BDVRSettings_Key = @"BDVRSettings";
  11. NSString *kSettingsKey = @"k_settings";
  12. static BDVRSettings *_sharedInstance = nil;
  13. @implementation BDVRSettings
  14. #pragma mark - Static Methods
  15. + (BDVRSettings *)getInstance
  16. {
  17. if (_sharedInstance == nil) {
  18. @synchronized(self) {
  19. if (_sharedInstance == nil) {
  20. _sharedInstance = [BDVRSettings loadBDVRSettings];
  21. }
  22. }
  23. }
  24. return _sharedInstance;
  25. }
  26. + (void)releaseInstance
  27. {
  28. if (_sharedInstance) {
  29. _sharedInstance = nil;
  30. }
  31. }
  32. + (BDVRSettings *)loadBDVRSettings
  33. {
  34. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  35. NSData *encodedObject = [defaults objectForKey:BDVRSettings_Key];
  36. if (encodedObject == nil) {
  37. return [[BDVRSettings alloc] init];
  38. } else {
  39. return [NSKeyedUnarchiver unarchiveObjectWithData:encodedObject];
  40. }
  41. }
  42. #pragma mark - Public Methods
  43. - (void)saveBDVRSettings
  44. {
  45. NSData *encodedObject = [NSKeyedArchiver archivedDataWithRootObject:_sharedInstance];
  46. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  47. [defaults setObject:encodedObject forKey:BDVRSettings_Key];
  48. [defaults synchronize];
  49. }
  50. - (void)configBDVRClient
  51. {
  52. if (_sharedInstance != nil) {
  53. for (BDVRSettingsItem *item in _sharedInstance.settingsItemArray) {
  54. if (item.curItemValue != nil) {
  55. switch (item.groupType) {
  56. case EBDGroupTypeASR: {
  57. if ([item.itemKey isEqualToString:(BDS_ASR_PROPERTY_LIST)]) {
  58. [[BDSEventManager createEventManagerWithName:BDS_ASR_NAME] setParameter:@[item.curItemValue]
  59. forKey:item.itemKey];
  60. } else {
  61. [[BDSEventManager createEventManagerWithName:BDS_ASR_NAME] setParameter:item.curItemValue
  62. forKey:item.itemKey];
  63. }
  64. break;
  65. }
  66. case EBDGroupTypeWakeup: {
  67. [[BDSEventManager createEventManagerWithName:BDS_WAKEUP_NAME] setParameter:item.curItemValue
  68. forKey:item.itemKey];
  69. break;
  70. }
  71. default:
  72. break;
  73. }
  74. }
  75. }
  76. }
  77. }
  78. - (id)getCurrentValueForKey:(NSString *)key
  79. {
  80. if (key.length) {
  81. for (BDVRSettingsItem *item in _sharedInstance.settingsItemArray) {
  82. if ([item.itemKey isEqualToString:key]) {
  83. return item.curItemValue;
  84. }
  85. }
  86. }
  87. return nil;
  88. }
  89. #pragma mark - Encoder & Decoder
  90. - (void)encodeWithCoder:(NSCoder *)encoder {
  91. [encoder encodeObject:self.settingsItemArray forKey:kSettingsKey];
  92. }
  93. - (id)initWithCoder:(NSCoder *)decoder {
  94. if((self = [super init])) {
  95. self.settingsItemArray = [decoder decodeObjectForKey:kSettingsKey];
  96. }
  97. return self;
  98. }
  99. - (instancetype)init {
  100. if (self = [super init]) {
  101. self.settingsItemArray = [[NSMutableArray alloc] initWithCapacity:1];
  102. [self.settingsItemArray addObject:[self createSampleRateItem]];
  103. [self.settingsItemArray addObject:[self createLanguageItem]];
  104. [self.settingsItemArray addObject:[self createToneItem]];
  105. [self.settingsItemArray addObject:[self createStrategyItem]];
  106. [self.settingsItemArray addObject:[self createEnableVADItem]];
  107. }
  108. return self;
  109. }
  110. #pragma mark - Common Settings
  111. - (BDVRSettingsItem *)createSampleRateItem {
  112. return [[BDVRSettingsItem alloc] initWithItemTitle:@"采样率"
  113. itemKey:(NSString *)BDS_ASR_SAMPLE_RATE
  114. groupType:EBDGroupTypeASR
  115. cellType:EBDCellTypeDetail
  116. curItemValue:@(EVoiceRecognitionRecordSampleRateAuto)
  117. detailDictionary:[NSDictionary dictionaryWithObjects:@[@"自适应", @"8K", @"16K"] forKeys:@[@(0), @(1), @(2)]]];
  118. }
  119. - (BDVRSettingsItem *)createLanguageItem {
  120. return [[BDVRSettingsItem alloc] initWithItemTitle:@"识别语言"
  121. itemKey:(NSString *)BDS_ASR_LANGUAGE
  122. groupType:EBDGroupTypeASR
  123. cellType:EBDCellTypeDetail
  124. curItemValue:@(EVoiceRecognitionLanguageChinese)
  125. detailDictionary:@{@0 : @"普通话", @1 : @"粤语", @2 : @"英文", @3 : @"四川话"}];
  126. }
  127. - (BDVRSettingsItem *)createToneItem {
  128. return [[BDVRSettingsItem alloc] initWithItemTitle:@"开启提示音"
  129. itemKey:(NSString *)BDS_ASR_PLAY_TONE
  130. groupType:EBDGroupTypeASR
  131. cellType:EBDCellTypeDetail
  132. curItemValue:@(0)
  133. detailDictionary:@{@0 : @"关闭", @(EVRPlayToneAll) : @"开启"}];
  134. }
  135. - (BDVRSettingsItem *)createStrategyItem {
  136. return [[BDVRSettingsItem alloc] initWithItemTitle:@"识别策略"
  137. itemKey:(NSString *)BDS_ASR_STRATEGY
  138. groupType:EBDGroupTypeASR
  139. cellType:EBDCellTypeDetail
  140. curItemValue:@(EVR_STRATEGY_ONLINE)
  141. detailDictionary:@{@0 : @"在线识别", @4 : @"离在线并行"}];
  142. }
  143. - (BDVRSettingsItem *)createEnableVADItem {
  144. return [[BDVRSettingsItem alloc] initWithItemTitle:@"开启端点检测"
  145. itemKey:(NSString *)BDS_ASR_ENABLE_LOCAL_VAD
  146. groupType:EBDGroupTypeASR
  147. cellType:EBDCellTypeDetail
  148. curItemValue:@(YES)
  149. detailDictionary:@{@NO : @"关闭", @YES : @"开启"}];
  150. }
  151. @end