Skip to content

Commit 7b311f0

Browse files
committed
fix: the global options overriding issue on getValidatedOptions
1 parent 5a1f404 commit 7b311f0

2 files changed

Lines changed: 23 additions & 47 deletions

File tree

android/src/main/java/com/speech/SpeechModule.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,9 +263,10 @@ class SpeechModule(reactContext: ReactApplicationContext) :
263263
}
264264

265265
private fun getValidatedOptions(options: ReadableMap): Map<String, Any> {
266-
val validated = mutableMapOf<String, Any>()
266+
val validated = globalOptions.toMutableMap()
267+
267268
if (options.hasKey("voice")) {
268-
validated["voice"] = options.getString("voice") ?: ""
269+
options.getString("voice")?.let { validated["voice"] = it }
269270
}
270271
if (options.hasKey("language")) {
271272
validated["language"] = options.getString("language")

ios/Speech.mm

Lines changed: 20 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,8 @@ - (instancetype)init {
3333
return self;
3434
}
3535

36-
- (void)configureAudioSessionWithOptions:(NSDictionary *)options {
36+
- (void)configureSilentModeSession:(NSString *)silentMode {
3737
NSError *error = nil;
38-
NSString *silentMode = options[@"silentMode"] ?: @"obey";
3938

4039
if ([silentMode isEqualToString:@"ignore"]) {
4140
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:&error];
@@ -63,8 +62,8 @@ - (NSDictionary *)getVoiceItem:(AVSpeechSynthesisVoice *)voice {
6362
}
6463

6564
- (NSDictionary *)getValidatedOptions:(VoiceOptions &)options {
66-
NSMutableDictionary *validatedOptions = [NSMutableDictionary new];
67-
65+
NSMutableDictionary *validatedOptions = [self.globalOptions mutableCopy];
66+
6867
if (options.voice()) {
6968
validatedOptions[@"voice"] = options.voice();
7069
}
@@ -90,22 +89,20 @@ - (NSDictionary *)getValidatedOptions:(VoiceOptions &)options {
9089
return validatedOptions;
9190
}
9291

93-
- (AVSpeechUtterance *)getDefaultUtterance:(NSString *)text {
92+
- (AVSpeechUtterance *)getUtterance:(NSString *)text withOptions:(NSDictionary *)options {
9493
AVSpeechUtterance *utterance = [[AVSpeechUtterance alloc] initWithString:text];
95-
96-
if (self.globalOptions[@"voice"]) {
97-
AVSpeechSynthesisVoice *voice = [AVSpeechSynthesisVoice voiceWithIdentifier:self.globalOptions[@"voice"]];
94+
95+
if (options[@"voice"]) {
96+
AVSpeechSynthesisVoice *voice = [AVSpeechSynthesisVoice voiceWithIdentifier:options[@"voice"]];
9897
if (voice) {
9998
utterance.voice = voice;
100-
} else if (self.globalOptions[@"language"]) {
101-
utterance.voice = [AVSpeechSynthesisVoice voiceWithLanguage:self.globalOptions[@"language"]];
10299
}
103-
} else {
104-
utterance.voice = [AVSpeechSynthesisVoice voiceWithLanguage:self.globalOptions[@"language"]];
100+
} else if (options[@"language"]) {
101+
utterance.voice = [AVSpeechSynthesisVoice voiceWithLanguage:options[@"language"]];
105102
}
106-
utterance.rate = [self.globalOptions[@"rate"] floatValue];
107-
utterance.volume = [self.globalOptions[@"volume"] floatValue];
108-
utterance.pitchMultiplier = [self.globalOptions[@"pitch"] floatValue];
103+
utterance.rate = [options[@"rate"] floatValue];
104+
utterance.volume = [options[@"volume"] floatValue];
105+
utterance.pitchMultiplier = [options[@"pitch"] floatValue];
109106

110107
return utterance;
111108
}
@@ -123,7 +120,7 @@ - (void)reset {
123120

124121
- (void)getAvailableVoices:(NSString *)language
125122
resolve:(RCTPromiseResolveBlock)resolve
126-
reject:(RCTPromiseRejectBlock)reject
123+
reject:(RCTPromiseRejectBlock)reject
127124
{
128125
NSMutableArray *voicesArray = [NSMutableArray new];
129126
NSArray *speechVoices = [AVSpeechSynthesisVoice speechVoices];
@@ -132,9 +129,7 @@ - (void)getAvailableVoices:(NSString *)language
132129
NSString *lowercaseLanguage = [language lowercaseString];
133130

134131
for (AVSpeechSynthesisVoice *voice in speechVoices) {
135-
NSString *voiceLanguage = [voice.language lowercaseString];
136-
137-
if ([voiceLanguage hasPrefix:lowercaseLanguage]) {
132+
if ([[voice.language lowercaseString] hasPrefix:lowercaseLanguage]) {
138133
[voicesArray addObject:[self getVoiceItem:voice]];
139134
}
140135
}
@@ -152,13 +147,12 @@ - (void)getEngines:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock
152147

153148
- (void)setEngine:(NSString *)engineName
154149
resolve:(RCTPromiseResolveBlock)resolve
155-
reject:(RCTPromiseRejectBlock)reject {
150+
reject:(RCTPromiseRejectBlock)reject {
156151
resolve(nil);
157152
}
158153

159154
- (void)isSpeaking:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject {
160-
BOOL speaking = self.synthesizer.isSpeaking;
161-
resolve(@(speaking));
155+
resolve(@(self.synthesizer.isSpeaking));
162156
}
163157

164158
- (void)stop:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject {
@@ -198,9 +192,9 @@ - (void)speak:(NSString *)text
198192
AVSpeechUtterance *utterance;
199193

200194
@try {
201-
[self configureAudioSessionWithOptions:self.globalOptions];
195+
[self configureSilentModeSession:self.globalOptions[@"silentMode"]];
202196

203-
utterance = [self getDefaultUtterance:text];
197+
utterance = [self getUtterance:text withOptions:self.globalOptions];
204198
[self.synthesizer speakUtterance:utterance];
205199
resolve(nil);
206200
}
@@ -224,28 +218,9 @@ - (void)speakWithOptions:(NSString *)text
224218

225219
@try {
226220
NSDictionary *validatedOptions = [self getValidatedOptions:options];
227-
[self configureAudioSessionWithOptions:validatedOptions];
221+
[self configureSilentModeSession:validatedOptions[@"silentMode"]];
228222

229-
utterance = [self getDefaultUtterance:text];
230-
231-
if (validatedOptions[@"voice"]) {
232-
AVSpeechSynthesisVoice *voice = [AVSpeechSynthesisVoice voiceWithIdentifier:validatedOptions[@"voice"]];
233-
if (voice) {
234-
utterance.voice = voice;
235-
}
236-
} else if (validatedOptions[@"language"]) {
237-
utterance.voice = [AVSpeechSynthesisVoice voiceWithLanguage:validatedOptions[@"language"]];
238-
}
239-
if (validatedOptions[@"pitch"]) {
240-
utterance.pitchMultiplier = [validatedOptions[@"pitch"] floatValue];
241-
}
242-
if (validatedOptions[@"volume"]) {
243-
utterance.volume = [validatedOptions[@"volume"] floatValue];
244-
}
245-
if (validatedOptions[@"rate"]) {
246-
utterance.rate = [validatedOptions[@"rate"] floatValue];
247-
}
248-
223+
utterance = [self getUtterance:text withOptions:validatedOptions];
249224
[self.synthesizer speakUtterance:utterance];
250225
resolve(nil);
251226
}

0 commit comments

Comments
 (0)