8
8
SafeAreaView ,
9
9
LayoutAnimation ,
10
10
} from "react-native" ;
11
- import { useEvent } from 'expo'
12
11
13
12
// We use Hume's low-level typescript SDK for this example.
14
13
// The React SDK (@humeai/voice-react) does not support React Native.
@@ -22,6 +21,7 @@ import { HumeClient, type Hume } from "hume";
22
21
// The provided native module is a good starting place, but you should
23
22
// modify it to fit the audio recording needs of your specific app.
24
23
import NativeAudio , { AudioEventPayload } from "./modules/audio" ;
24
+ import VoiceIsolationModePrompt from "./VoiceIsolationModePrompt" ;
25
25
26
26
// Represents a chat message in the chat display.
27
27
interface ChatEntry {
@@ -55,6 +55,8 @@ const App = () => {
55
55
const [ isConnected , setIsConnected ] = useState ( false ) ;
56
56
const [ isMuted , setIsMuted ] = useState ( false ) ;
57
57
const [ chatEntries , setChatEntries ] = useState < ChatEntry [ ] > ( [ ] ) ;
58
+ const [ showVoiceIsolationPrompt , setShowVoiceIsolationPrompt ] = useState ( false ) ;
59
+ const [ currentMicMode , setCurrentMicMode ] = useState ( "Standard" ) ;
58
60
const humeRef = useRef < HumeClient | null > ( null ) ;
59
61
const addChatEntry = ( entry : ChatEntry ) => {
60
62
setChatEntries ( ( prev ) => [ ...prev , entry ] ) ;
@@ -95,6 +97,14 @@ const App = () => {
95
97
return ;
96
98
}
97
99
100
+ const micMode = await NativeAudio . getMicrophoneMode ( ) ;
101
+ setCurrentMicMode ( micMode ) ;
102
+
103
+ if ( micMode !== "N/A" && micMode !== "Voice Isolation" ) {
104
+ setShowVoiceIsolationPrompt ( true ) ;
105
+ return
106
+ }
107
+
98
108
const chatSocket = hume . empathicVoice . chat . connect ( {
99
109
configId : process . env . EXPO_PUBLIC_HUME_CONFIG_ID ,
100
110
} ) ;
@@ -142,50 +152,42 @@ const App = () => {
142
152
} ;
143
153
144
154
const handleDisconnect = async ( ) => {
155
+ if ( chatSocketRef . current ) {
156
+ chatSocketRef . current . close ( ) ;
157
+ chatSocketRef . current = null ;
158
+ }
145
159
try {
146
160
await NativeAudio . stopRecording ( ) ;
147
- await NativeAudio . stopPlayback ( ) ;
148
161
} catch ( error ) {
149
162
console . error ( "Error while stopping recording" , error ) ;
150
163
}
151
- if ( chatSocketRef . current ) {
152
- chatSocketRef . current . close ( ) ;
153
- }
164
+
165
+ await NativeAudio . stopPlayback ( ) ;
154
166
} ;
155
167
156
168
useEffect ( ( ) => {
157
169
if ( isConnected ) {
158
- handleConnect ( ) . catch ( ( error ) => {
159
- console . error ( "Error while connecting:" , error ) ;
160
- } ) ;
170
+ handleConnect ( )
161
171
} else {
162
- handleDisconnect ( ) . catch ( ( error ) => {
163
- console . error ( "Error while disconnecting:" , error ) ;
164
- } ) ;
172
+ handleDisconnect ( )
165
173
}
166
174
const onUnmount = ( ) => {
167
- NativeAudio . stopRecording ( ) . catch ( ( error : any ) => {
168
- console . error ( "Error while stopping recording" , error ) ;
169
- } ) ;
170
- if (
171
- chatSocketRef . current &&
172
- chatSocketRef . current . readyState === WebSocket . OPEN
173
- ) {
174
- chatSocketRef . current ?. close ( ) ;
175
+ if ( chatSocketRef . current ) {
176
+ chatSocketRef . current . close ( ) ;
177
+ chatSocketRef . current = null ;
175
178
}
179
+
180
+ NativeAudio . stopRecording ( ) ;
181
+ NativeAudio . stopPlayback ( ) ;
176
182
} ;
177
183
return onUnmount ;
178
184
} , [ isConnected ] ) ;
179
185
180
186
useEffect ( ( ) => {
181
187
if ( isMuted ) {
182
- NativeAudio . mute ( ) . catch ( ( error ) => {
183
- console . error ( "Error while muting" , error ) ;
184
- } ) ;
188
+ NativeAudio . mute ( ) ;
185
189
} else {
186
- NativeAudio . unmute ( ) . catch ( ( error ) => {
187
- console . error ( "Error while unmuting" , error ) ;
188
- } ) ;
190
+ NativeAudio . unmute ( ) ;
189
191
}
190
192
} , [ isMuted ] ) ;
191
193
@@ -290,6 +292,12 @@ const App = () => {
290
292
/>
291
293
</ View >
292
294
</ SafeAreaView >
295
+
296
+ < VoiceIsolationModePrompt
297
+ isVisible = { showVoiceIsolationPrompt }
298
+ currentMode = { currentMicMode }
299
+ onDismiss = { ( ) => setShowVoiceIsolationPrompt ( false ) }
300
+ />
293
301
</ View >
294
302
) ;
295
303
} ;
0 commit comments