-
Notifications
You must be signed in to change notification settings - Fork 52
/
SpeechSynthesizer.m
151 lines (122 loc) · 3.7 KB
/
SpeechSynthesizer.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#import "SpeechSynthesizer.h"
#import "RCTUtils.h"
#import "RCTLog.h"
@implementation SpeechSynthesizer
RCT_EXPORT_MODULE()
// Speak
RCT_EXPORT_METHOD(speakUtterance:(NSDictionary *)args callback:(RCTResponseSenderBlock)callback)
{
// Error if self.synthesizer was already initialized
if (self.synthesizer) {
return callback(@[RCTMakeError(@"There is a speech in progress. Use the `paused` method to know if it's paused.", nil, nil)]);
}
// Set args to variables
NSString *text = args[@"text"];
NSString *voice = args[@"voice"];
NSNumber *rate = args[@"rate"];
// Error if no text is passed
if (!text) {
RCTLogError(@"[Speech] You must specify a text to speak.");
return;
}
// Set default voice
NSString *voiceLanguage;
// Set voice if provided
if (voice) {
voiceLanguage = voice;
// Fallback to en-US
} else {
voiceLanguage = @"en-US";
}
// Setup utterance and voice
AVSpeechUtterance *utterance = [[AVSpeechUtterance alloc] initWithString:text];
utterance.voice = [AVSpeechSynthesisVoice voiceWithLanguage:voiceLanguage];
if (rate) {
utterance.rate = [rate doubleValue];
}
self.synthesizer = [[AVSpeechSynthesizer alloc] init];
self.synthesizer.delegate = self;
// Speak
[self.synthesizer speakUtterance:utterance];
// Return that the speach has started
callback(@[[NSNull null], @true]);
}
// Stops synthesizer
RCT_EXPORT_METHOD(stopSpeakingAtBoundary)
{
if (self.synthesizer) {
[self.synthesizer stopSpeakingAtBoundary:AVSpeechBoundaryImmediate];
}
}
// Pauses synthesizer
RCT_EXPORT_METHOD(pauseSpeakingAtBoundary)
{
if (self.synthesizer) {
[self.synthesizer pauseSpeakingAtBoundary:AVSpeechBoundaryImmediate];
}
}
// Resumes synthesizer
RCT_EXPORT_METHOD(continueSpeakingAtBoundary)
{
if (self.synthesizer) {
[self.synthesizer continueSpeaking];
}
}
// Returns false if synthesizer is paued
RCT_EXPORT_METHOD(paused:(RCTResponseSenderBlock)callback)
{
if (self.synthesizer.paused) {
callback(@[@true]);
} else {
callback(@[@false]);
}
}
// Returns true if synthesizer is speaking
RCT_EXPORT_METHOD(speaking:(RCTResponseSenderBlock)callback)
{
if (self.synthesizer.speaking) {
callback(@[@true]);
} else {
callback(@[@false]);
}
}
// Returns available voices
RCT_EXPORT_METHOD(speechVoices:(RCTResponseSenderBlock)callback)
{
NSArray *speechVoices = [AVSpeechSynthesisVoice speechVoices];
NSArray *locales = [speechVoices valueForKey:@"language"];
callback(@[[NSNull null], locales]);
}
// Delegate
// Finished Handler
-(void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didFinishSpeechUtterance:(AVSpeechUtterance *)utterance
{
NSLog(@"Speech finished");
self.synthesizer = nil;
}
// Started Handler
-(void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didStartSpeechUtterance:(AVSpeechUtterance *)utterance
{
NSLog(@"Speech started");
}
// Paused Handler
-(void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didPauseSpeechUtterance:(AVSpeechUtterance *)utterance
{
NSLog(@"Speech paused");
}
// Resumed Handler
-(void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didContinueSpeechUtterance:(AVSpeechUtterance *)utterance
{
NSLog(@"Speech resumed");
}
// Word Handler
-(void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer willSpeakRangeOfSpeechString:(NSRange)characterRange utterance:(AVSpeechUtterance *)utterance
{
NSLog(@"Started word");
}
// Cancelled Handler
-(void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didCancelSpeechUtterance:(AVSpeechUtterance *)utterance
{
NSLog(@"Speech cancelled");
}
@end