-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathRootView.tsx
More file actions
173 lines (154 loc) · 4.86 KB
/
RootView.tsx
File metadata and controls
173 lines (154 loc) · 4.86 KB
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import React from 'react';
import {gs} from '../styles/gs';
import {
Text,
View,
Alert,
Platform,
StyleSheet,
useColorScheme,
} from 'react-native';
import Speech, {
HighlightedText,
type HighlightedSegmentArgs,
type HighlightedSegmentProps,
} from '@mhpdev/react-native-speech';
import Button from '../components/Button';
import {SafeAreaView} from 'react-native-safe-area-context';
const isAndroidLowerThan26 = Platform.OS === 'android' && Platform.Version < 26;
const Introduction =
"This high-performance text-to-speech library is built for bare React Native and Expo, compatible with Android and iOS's new architecture (default from React Native 0.76). It enables seamless speech management with start, pause, resume, and stop controls, and provides events for detailed synthesis management.";
const RootView: React.FC = () => {
const scheme = useColorScheme();
const textColor = scheme === 'dark' ? 'white' : 'black';
const [isPaused, setIsPaused] = React.useState<boolean>(false);
const [isStarted, setIsStarted] = React.useState<boolean>(false);
const [highlights, setHighlights] = React.useState<
Array<HighlightedSegmentProps>
>([]);
const targetId = React.useRef<string>('');
React.useEffect(() => {
// Speech.configure({silentMode: 'obey', ducking: true});
const onSpeechEnd = () => {
setIsStarted(false);
setIsPaused(false);
setHighlights([]);
targetId.current = '';
};
const startSubscription = Speech.onStart(({id}) => {
if (id === targetId.current) {
setIsStarted(true);
console.log(`Speech ${id} started`);
}
});
const finishSubscription = Speech.onFinish(({id}) => {
if (id === targetId.current) {
onSpeechEnd();
console.log(`Speech ${id} finished`);
}
});
const pauseSubscription = Speech.onPause(({id}) => {
if (id === targetId.current) {
setIsPaused(true);
console.log(`Speech ${id} paused`);
}
});
const resumeSubscription = Speech.onResume(({id}) => {
if (id === targetId.current) {
setIsPaused(false);
console.log(`Speech ${id} resumed`);
}
});
const stoppedSubscription = Speech.onStopped(({id}) => {
if (id === targetId.current) {
onSpeechEnd();
console.log(`Speech ${id} stopped`);
}
});
const progressSubscription = Speech.onProgress(({id, location, length}) => {
setHighlights([
{
start: location,
end: location + length,
},
]);
console.log(
`Speech ${id} progress, current word length: ${length}, current char position: ${location}`,
);
});
// (async () => {
// const enVoices = await Speech.getAvailableVoices('en-us');
// Speech.configure({
// rate: 0.5,
// volume: 1,
// voice: enVoices[3]?.identifier,
// });
// })();
// (async () => {
// const engines = await Speech.getEngines();
// if (engines?.[0]) {
// await Speech.setEngine(engines[0].name);
// }
// })();
return () => {
startSubscription.remove();
finishSubscription.remove();
pauseSubscription.remove();
resumeSubscription.remove();
stoppedSubscription.remove();
progressSubscription.remove();
};
}, []);
const onStartPress = React.useCallback(async () => {
const id = await Speech.speak(Introduction);
targetId.current = id;
}, []);
const onHighlightedPress = React.useCallback(
({text, start, end}: HighlightedSegmentArgs) =>
Alert.alert(
'Highlighted',
`The current segment is "${text}", starting at ${start} and ending at ${end}`,
),
[],
);
return (
<SafeAreaView style={[gs.flex, gs.p10]}>
<View style={gs.flex}>
<Text style={[gs.title, {color: textColor}]}>Introduction</Text>
<HighlightedText
text={Introduction}
highlights={highlights}
highlightedStyle={styles.highlighted}
onHighlightedPress={onHighlightedPress}
style={[gs.paragraph, {color: textColor}]}
/>
</View>
<View style={[gs.row, gs.p10]}>
<Button label="Start" disabled={isStarted} onPress={onStartPress} />
<Button label="Stop" disabled={!isStarted} onPress={Speech.stop} />
{isAndroidLowerThan26 ? null : (
<React.Fragment>
<Button
label="Pause"
onPress={Speech.pause}
disabled={isPaused || !isStarted}
/>
<Button
label="Resume"
disabled={!isPaused}
onPress={Speech.resume}
/>
</React.Fragment>
)}
</View>
</SafeAreaView>
);
};
export default RootView;
const styles = StyleSheet.create({
highlighted: {
color: 'black',
fontWeight: '600',
backgroundColor: '#ffff00',
},
});