voice: [Android][expo] [TypeError: Cannot read property 'startSpeech' of null]
Hello, I have imported the library into my project, but when I try to launch the “start” or “stop” function, I get this error:
TypeError: Cannot read property 'startSpeech' of null
TypeError: Cannot read property 'stopSpeech' of null
In app.json, here is my configuration:
{ "expo": { [....] "android": { "adaptiveIcon": { "foregroundImage": "./assets/images/adaptive-icon.png", "backgroundColor": "#ffffff" }, "permissions": [ "android.permission.RECORD_AUDIO" ] }, "plugins": [ [ "@react-native-voice/voice", { "microphonePermission": "CUSTOM: Allow $(PRODUCT_NAME) to access the microphone", "speechRecognitionPermission": "CUSTOM: Allow $(PRODUCT_NAME) to securely recognize user speech" } ] ] } }
Here’s the code for voice recording:
`import React, { useState } from ‘react’; import { StyleSheet, Text, TouchableOpacity, View } from ‘react-native’; import Voice from ‘@react-native-voice/voice’;
const SpeechToText = () => { const [isRecording, setIsRecording] = useState(false); const [speechText, setSpeechText] = useState(‘’);
const onSpeechStartHandler = () => {
setIsRecording(true);
setSpeechText('');
};
const onSpeechEndHandler = () => {
setIsRecording(false);
};
const onSpeechResultsHandler = (event: any) => {
setSpeechText(event.value[0]);
};
const startSpeechToText = async () => {
try {
await Voice.start('en-US');
onSpeechStartHandler();
} catch (error) {
console.log(error);
}
};
const stopSpeechToText = async () => {
try {
await Voice.stop();
onSpeechEndHandler();
} catch (error) {
console.log(error);
}
};
return (
<View style={styles.container}>
<TouchableOpacity
style={styles.button}
onPressIn={startSpeechToText}
onPressOut={stopSpeechToText}
disabled={isRecording}
>
<Text style={styles.text}>Press and hold to speak</Text>
</TouchableOpacity>
<Text style={styles.speechText}>{speechText}</Text>
</View>
);
}; `
I have tried everything, but I don’t understand why the library doesn’t seem to load correctly… I am using Android 13, here is my package.json:
{ "scripts": { "start": "expo start", "android": "expo start --android", "ios": "expo start --ios", "web": "expo start --web", "test": "jest --watchAll" }, "dependencies": { "@react-native-voice/voice": "^3.2.4", "expo": "~48.0.11", "expo-font": "~11.1.1", "expo-linking": "~4.0.1", "expo-permissions": "^14.1.1", "expo-router": "^1.5.2", "expo-speech": "^11.1.1", "expo-splash-screen": "~0.18.1", "expo-status-bar": "~1.4.4", "expo-system-ui": "~2.2.1", "expo-web-browser": "~12.1.1", "react": "18.2.0", "react-dom": "18.2.0", "react-native": "0.71.6", "react-native-keyboard-aware-scroll-view": "^0.9.5", "react-native-safe-area-context": "4.5.0", "react-native-screens": "~3.20.0", "react-native-vector-icons": "^9.2.0", "react-native-web": "~0.18.10" }, "devDependencies": { "@babel/core": "^7.20.0", "@types/react": "~18.0.14", "@types/react-native": "^0.71.5", "react-test-renderer": "18.2.0", "typescript": "^4.9.4" }, "private": true }
Perhaps there is a version incompatibility?
About this issue
- Original URL
- State: open
- Created 4 years ago
- Reactions: 3
- Comments: 17
I have the same issue
Expo Speech To Text is ready now. Hope everyone could give a try and report bugs (if have) and improve it with me. https://github.com/anhtuank7c/expo-stt
I am working on rewrite this library for Expo, I need a bit help to release it. https://github.com/anhtuank7c/expo-stt Hope it can be finish in recent week
Hey everyone,
I also faced this issue and, after several attempts to fix it, I decided to replace to ‘expo-speech-recognition’. It worked perfectly for my use case without any additional configuration.
If you’re still struggling with this issue, I recommend giving expo-speech-recognition a try. Let me know if you need any help with the migration!