Skip to content

Commit

Permalink
Merge pull request #94 from phuongwd/ignoreSilentSwitch
Browse files Browse the repository at this point in the history
Controls the iOS silent switch behavior
  • Loading branch information
ak1394 authored Apr 14, 2019
2 parents b2f0676 + 2e46335 commit 2675bb7
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
18 changes: 15 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ Tts.speak('Hello, world!', { androidParams: { KEY_PARAM_PAN: -1, KEY_PARAM_VOLUM

For more detail on `androidParams` properties, please take a look at [official android documentation](https://developer.android.com/reference/android/speech/tts/TextToSpeech.Engine.html). Please note that there are still unsupported key with this wrapper library such as `KEY_PARAM_SESSION_ID`. The following are brief summarization of currently implemented keys:

- `KEY_PARAM_PAN` ranges from `-1` to `+1`.
- `KEY_PARAM_PAN` ranges from `-1` to `+1`.

- `KEY_PARAM_VOLUME` ranges from `0` to `1`, where 0 means silence. Note that `1` is a default value for Android.

Expand Down Expand Up @@ -75,7 +75,7 @@ Tts.setDucking(true);

### List Voices

Returns list of available voices
Returns list of available voices

*(not supported on Android API Level < 21, returns empty list)*

Expand Down Expand Up @@ -108,7 +108,7 @@ Tts.setDefaultLanguage('en-IE');

### Set default Voice

Sets default voice, pass one of the voiceId as reported by a call to Tts.voices()
Sets default voice, pass one of the voiceId as reported by a call to Tts.voices()

*(not available on Android API Level < 21)*

Expand Down Expand Up @@ -140,6 +140,18 @@ Sets default pitch. The pitch parameter is a float where where 1.0 is a normal p
Tts.setDefaultPitch(1.5);
```

### Controls the iOS silent switch behavior

Platforms: iOS

- "inherit" (default) - Use the default behavior
- "ignore" - Play audio even if the silent switch is set
- "obey" - Don't play audio if the silent switch is set

```js
Tts.setIgnoreSilentSwitch("ignore");
```

### Events

Subscribe to TTS events
Expand Down
8 changes: 7 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class Tts extends NativeEventEmitter {
if (Platform.OS === 'ios') {
return Promise.resolve(true);
}
return TextToSpeech.requestInstallData();
return TextToSpeech.requestInstallData();
}

setDucking(enabled) {
Expand All @@ -48,6 +48,12 @@ class Tts extends NativeEventEmitter {
return TextToSpeech.setDefaultLanguage(language);
}

setIgnoreSilentSwitch(ignoreSilentSwitch) {
if (Platform.OS === "ios" && ignoreSilentSwitch) {
return TextToSpeech.setIgnoreSilentSwitch(ignoreSilentSwitch);
}
}

voices() {
return TextToSpeech.voices();
}
Expand Down
21 changes: 20 additions & 1 deletion ios/TextToSpeech/TextToSpeech.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@

#import "TextToSpeech.h"

@implementation TextToSpeech
@implementation TextToSpeech {
NSString * _ignoreSilentSwitch;
}

@synthesize bridge = _bridge;

Expand All @@ -30,6 +32,7 @@ -(instancetype)init
_synthesizer = [AVSpeechSynthesizer new];
_synthesizer.delegate = self;
_ducking = false;
_ignoreSilentSwitch = @"inherit"; // inherit, ignore, obey
}

return self;
Expand Down Expand Up @@ -65,6 +68,12 @@ + (BOOL)requiresMainQueueSetup
if (_defaultPitch) {
utterance.pitchMultiplier = _defaultPitch;
}

if([_ignoreSilentSwitch isEqualToString:@"ignore"]) {
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
} else if([_ignoreSilentSwitch isEqualToString:@"obey"]) {
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:nil];
}

[self.synthesizer speakUtterance:utterance];
resolve([NSNumber numberWithUnsignedLong:utterance.hash]);
Expand Down Expand Up @@ -178,6 +187,16 @@ + (BOOL)requiresMainQueueSetup
}
}

RCT_EXPORT_METHOD(setIgnoreSilentSwitch:(NSString *)ignoreSilentSwitch
resolve:(RCTPromiseResolveBlock)resolve
reject:(RCTPromiseRejectBlock)reject)
{
if(ignoreSilentSwitch) {
_ignoreSilentSwitch = ignoreSilentSwitch;
resolve(@"success");
}
}

RCT_EXPORT_METHOD(voices:(RCTPromiseResolveBlock)resolve
reject:(__unused RCTPromiseRejectBlock)reject)
{
Expand Down

0 comments on commit 2675bb7

Please sign in to comment.