react-native-voice / voice

:microphone: React Native Voice Recognition library for iOS and Android (Online and Offline Support)
MIT License
1.8k stars 485 forks source link

[Android][expo] [TypeError: Cannot read property 'startSpeech' of null] #434

Open theobaronnat opened 1 year ago

theobaronnat commented 1 year ago

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?

BRUT4LxD commented 1 year ago

I have the same issue

anhtuank7c commented 1 year ago

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

theobaronnat commented 1 year ago

I can try using your library with my project to see if it works, but unfortunately, I can't review your code because I'm not an expert in react-native/expo. I exclusively code using Vue.js/Nest.js or PHP/Symfony, so I'm not familiar with the best practices and conventions related to react-native/expo.

anhtuank7c commented 1 year ago

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

John8844 commented 6 months ago

I have set all you said above, like added dependency and plugin also added. But I have one doubt "What is ${productName} " and My error is "Type Error: Cannot read property 'startSpeech' of null".

import { StyleSheet, Text, View, TouchableOpacity } from 'react-native'; import Voice from '@react-native-voice/voice'; import { useState } from 'react';

const VoiceSearch = () => { const [result, setResult] = useState(''); const [error, setError] = useState(''); const [isRecording, setIsRecording] = useState(false);

Voice.onSpeechStart = () => setIsRecording(true);
Voice.onSpeechEnd = () => setIsRecording(false);
Voice.onSpeechError = (err) => setError(err.error);
Voice.onSpeechResults = (result) => setResult(result.value[0]);

const startRecording = async() => {
    try {
        await Voice.start('en-US');
    } catch (err) {
        setError(err);
    }
};
const stopRecording = async() => {
    try {
        await Voice.stop();
    } catch (err) {
        setError(err)
    }
};

return (
    <View style={styles.container}>
        <Text style={styles.voiceInput}>
            VoiceSearch
        </Text>
        <Text>{result}</Text>
        <Text>{error.toString()}</Text>
        <View style={styles.voiceStartContainer}>
            <TouchableOpacity style={styles.mike} onPress={isRecording ? stopRecording : startRecording}>
                <Text style={{color: 'red', textAlign: 'center'}}>{isRecording ? 'Stop Recording' : 'Start Recording'}</Text>
            </TouchableOpacity>
        </View>
    </View>
)

} const styles = StyleSheet.create({ container: { width: '100%', paddingHorizontal: 20, paddingVertical: 30 }, voiceInput: { textAlign: 'center', fontSize: 20, fontWeight: 'bold', color: 'green' }, voiceStartContainer: { width: '100%', paddingHorizontal: 20, paddingVertical: 25, justifyContent: 'center', alignItems: 'center' }, mike: { width: 300, height: 50, } }) export default VoiceSearch;

BrenoVital commented 5 months ago

Did anyone get the error?

vinhdnvn commented 4 months ago

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

Thanks for a lot, countrymen <3

ahadnawaz585 commented 3 months ago

is there any one with a solution ?

BrenoVital commented 3 months ago

existe alguém com uma solução?

I didn't find the solution but I'm using expo's own voice in SDK 51 and got the same result