spokestack / react-native-spokestack

Spokestack: give your React Native app a voice interface!
https://spokestack.io
Apache License 2.0
56 stars 13 forks source link

"Illegal callback invocation error", "Spokestack error during initialize.", and app crash when calling Spokestack.initialize() on first load. No issues on subsequent loads. (iOS only) #105

Open cdmoss opened 2 years ago

cdmoss commented 2 years ago

Describe the bug

As the title describes, here is the the console output during the first load of the app on an iOS device:

2021-11-16 13:08:06.096795-0700 mobile[4246:3045067] [native] Illegal callback invocation from native module. This callback type only permits a single invocation from native code.
2021-11-16 13:08:06.096902-0700 mobile[4246:3045068] [native] Illegal callback invocation from native module. This callback type only permits a single invocation from native code.
2021-11-16 13:08:06.423242-0700 mobile[4246:3044487] [javascript] [Error: Spokestack error during initialize.]
2021-11-16 13:08:08.461274-0700 mobile[4246:3044487] [javascript] Possible Unhandled Promise Rejection (id: 0):
Error: Speech Pipeline is not initialized. Call Spokestack.initialize() first.

followed by the app crashing.

Strangely, after reloading the app, these issues do not occur and the app works fine.

Here is the code initalizing Spokestack:

useEffect(() => {
        const initializeSpokestack = () => {
            Spokestack.addEventListener('activate', () => {
                console.log('Spokestack activated')
                if (!navigation.isFocused()) {
                    navigation.navigate('Map')
                }
                if (AppState.currentState != 'active') {
                    Linking.openURL("cnp.mobile://")
                }
                if (!voiceVisible) {
                    toggleVoiceVisible(true)   
                }
                setSpeechResult('')
                setListening(true)
            });
            Spokestack.addEventListener('deactivate', () => {
                console.log('Spokestack deactivated')
                setListening(false)
            })
            Spokestack.addEventListener('recognize', ({transcript}) => {
                console.log('Spokestack finished recognizing')
                setPartialSpeechResult('')
                setSpeechResult((transcript as string).trim().toLowerCase())
                setPreviousResult(transcript)
            })
            Spokestack.addEventListener('partial_recognize', ({transcript}) => {
                console.log('Spokestack recognizing')
                setPartialSpeechResult(transcript)
            })  

            try {
                Spokestack.initialize(
                    SPOKESTACK_ID, // account id
                    SPOKESTACK_TOKEN, // api token
                    {
                        pipeline: {
                            profile: PipelineProfile.TFLITE_WAKEWORD_NATIVE_ASR
                        },
                        wakeword: {
                            filter: require('../../../../assets/tflites/filter.tflite'),
                            detect: require('../../../../assets/tflites/detect.tflite'),
                            encode: require('../../../../assets/tflites/encode.tflite'),
                        },
                    }
                ).then(Spokestack.start).catch(error => console.log(error))
            } catch (error) {
                console.log(error)
            }
        }
        initializeSpokestack()

        return () => {
            Spokestack.removeAllListeners();
        };
    }, []);

Your environment

Expected behavior

The app should not crash on first load.

Actual behavior

The app crashes on first load with aforementioned errors.

Has anyone encountered this sort of error, or know what could cause it?