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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
| | import React, { useCallback, useEffect, useRef, useState } from "react";
| | import { Animated, Pressable, Text, View } from "react-native";
| | import * as Haptics from "expo-haptics";
| | import {
| | ExpoSpeechRecognitionModule,
| | useSpeechRecognitionEvent,
| | } from "expo-speech-recognition";
| |
| | interface VoiceButtonProps {
| | onTranscript: (text: string) => void;
| | }
| |
| | const VOICE_BUTTON_SIZE = 72;
| |
| | /**
| | * Tap-to-toggle voice button using on-device speech recognition.
| | * - Tap once: start listening
| | * - Tap again: stop and send transcript
| | * - Long-press while listening: cancel (discard)
| | */
| | export function VoiceButton({ onTranscript }: VoiceButtonProps) {
| | const [isListening, setIsListening] = useState(false);
| | const [transcript, setTranscript] = useState("");
| | const pulseAnim = useRef(new Animated.Value(1)).current;
| | const glowAnim = useRef(new Animated.Value(0)).current;
| | const pulseLoop = useRef<Animated.CompositeAnimation | null>(null);
| | const cancelledRef = useRef(false);
| |
| | // Speech recognition events
| | useSpeechRecognitionEvent("start", () => {
| | setIsListening(true);
| | });
| |
| | useSpeechRecognitionEvent("end", () => {
| | setIsListening(false);
| | stopPulse();
| |
| | // Send transcript if we have one and weren't cancelled
| | if (!cancelledRef.current && transcript.trim()) {
| | onTranscript(transcript.trim());
| | }
| | setTranscript("");
| | cancelledRef.current = false;
| | });
| |
| | useSpeechRecognitionEvent("result", (event) => {
| | const text = event.results[0]?.transcript ?? "";
| | setTranscript(text);
| | });
| |
| | useSpeechRecognitionEvent("error", (event) => {
| | console.error("Speech recognition error:", event.error, event.message);
| | setIsListening(false);
| | stopPulse();
| | setTranscript("");
| | });
| |
| | const startPulse = useCallback(() => {
| | pulseLoop.current = Animated.loop(
| | Animated.sequence([
| | Animated.timing(pulseAnim, {
| | toValue: 1.15,
| | duration: 700,
| | useNativeDriver: true,
| | }),
| | Animated.timing(pulseAnim, {
| | toValue: 1,
| | duration: 700,
| | useNativeDriver: true,
| | }),
| | ])
| | );
| | pulseLoop.current.start();
| | Animated.timing(glowAnim, {
| | toValue: 1,
| | duration: 300,
| | useNativeDriver: true,
| | }).start();
| | }, [pulseAnim, glowAnim]);
| |
| | const stopPulse = useCallback(() => {
| | pulseLoop.current?.stop();
| | pulseAnim.setValue(1);
| | Animated.timing(glowAnim, {
| | toValue: 0,
| | duration: 200,
| | useNativeDriver: true,
| | }).start();
| | }, [pulseAnim, glowAnim]);
| |
| | const startListening = useCallback(async () => {
| | const result = await ExpoSpeechRecognitionModule.requestPermissionsAsync();
| | if (!result.granted) return;
| |
| | cancelledRef.current = false;
| | setTranscript("");
| | startPulse();
| |
| | ExpoSpeechRecognitionModule.start({
| | lang: "en-US",
| | interimResults: true,
| | continuous: true,
| | });
| | }, [startPulse]);
| |
| | const stopAndSend = useCallback(() => {
| | stopPulse();
| | cancelledRef.current = false;
| | ExpoSpeechRecognitionModule.stop();
| | }, [stopPulse]);
| |
| | const cancelListening = useCallback(() => {
| | Haptics.notificationAsync(Haptics.NotificationFeedbackType.Warning);
| | stopPulse();
| | cancelledRef.current = true;
| | setTranscript("");
| | ExpoSpeechRecognitionModule.abort();
| | }, [stopPulse]);
| |
| | const handleTap = useCallback(async () => {
| | Haptics.impactAsync(Haptics.ImpactFeedbackStyle.Medium);
| | if (isListening) {
| | stopAndSend();
| | } else {
| | await startListening();
| | }
| | }, [isListening, stopAndSend, startListening]);
| |
| | const handleLongPress = useCallback(() => {
| | if (isListening) {
| | cancelListening();
| | }
| | }, [isListening, cancelListening]);
| |
| | return (
| | <View style={{ alignItems: "center", justifyContent: "center" }}>
| | {/* Outer pulse ring */}
| | <Animated.View
| | style={{
| | position: "absolute",
| | width: VOICE_BUTTON_SIZE + 24,
| | height: VOICE_BUTTON_SIZE + 24,
| | borderRadius: (VOICE_BUTTON_SIZE + 24) / 2,
| | backgroundColor: isListening ? "rgba(255, 159, 67, 0.12)" : "transparent",
| | transform: [{ scale: pulseAnim }],
| | opacity: glowAnim,
| | }}
| | />
| |
| | {/* Button */}
| | <Pressable
| | onPress={handleTap}
| | onLongPress={handleLongPress}
| | delayLongPress={600}
| | >
| | <View
| | style={{
| | width: VOICE_BUTTON_SIZE,
| | height: VOICE_BUTTON_SIZE,
| | borderRadius: VOICE_BUTTON_SIZE / 2,
| | backgroundColor: isListening ? "#FF9F43" : "#4A9EFF",
| | alignItems: "center",
| | justifyContent: "center",
| | shadowColor: isListening ? "#FF9F43" : "#4A9EFF",
| | shadowOffset: { width: 0, height: 4 },
| | shadowOpacity: 0.4,
| | shadowRadius: 12,
| | elevation: 8,
| | }}
| | >
| | <Text style={{ fontSize: 28 }}>{isListening ? "⏹" : "🎤"}</Text>
| | </View>
| | </Pressable>
| |
| | {/* Label / transcript preview */}
| | <Text
| | style={{
| | color: isListening ? "#FF9F43" : "#5A5A78",
| | fontSize: 11,
| | marginTop: 4,
| | fontWeight: isListening ? "600" : "400",
| | maxWidth: 200,
| | textAlign: "center",
| | }}
| | numberOfLines={2}
| | >
| | {isListening
| | ? transcript || "Listening..."
| | : "Tap to talk"}
| | </Text>
| | </View>
| | );
| | }
|
|