Expo Speech callbacks not getting invoked

Please provide the following:

  1. SDK Version: ~37.0.3
  2. Platforms(Android/iOS/web/all): Android Simulator , IOS Simulator
    “expo-speech”: “~8.1.0”

I am using expo speech in my application.
For my code speak callbacks are not invoked.
Otherwise speech functionality works fine.

Please first have a look at the code below.
So console.log(“Complete triggered”); or console.log(“Start triggered”); never get invoked.
Am i doing something very silly? Any help will be much appreciated.

I have attached a full copy of the code that I am running.

import * as React from "react";
import { Text } from "react-native";
import * as Speech from "expo-speech";
import { TimedWorkout, WorkoutIntensity, IStrikeCombination } from "../Entities/TimedWorkout";
import RandomCombinationSelector from "../Services/RandomCombinationSelector";
import { ITimedActivity, TimedActivityType } from "../Entities/TimedActivity";

export interface IWorkoutSpeechProps {
    workout: TimedWorkout,
    inProgress: boolean,
    currentActivity?: ITimedActivity,
    onNewCombination?: (combination: IStrikeCombination) => void
}

export const WorkoutCombinationSpeech = (props: IWorkoutSpeechProps) => {
    const [currentCombination, setCurrentCombination] = React.useState("");
    const [waiting, setWaiting] = React.useState(false);
    const [speaking, setSpeaking] = React.useState(false);

    React.useEffect(() => {
        const startWait = () => {
            setWaiting(true);
            const waitTime = getWaitTimeInMilliseconds();
            setTimeout(() => {
                setWaiting(false);
            }, waitTime);
        }

        const start = () => {
            console.log("start triggered");
          setSpeaking(true);
        };
        const complete = () => {
            console.log("Complete triggered");
          setSpeaking(false);
        };
    
        const _speak = (textToSpeak: string) => {
            console.log("Speaking Started");
            //TODO: Fix the callbacks
            Speech.speak(textToSpeak, {
              pitch: 1,
              rate: 0.8,
              onStart: start,
              onDone: complete,
              onStopped: complete,
              onError: complete,
            });
            console.log("Speaking is done");
          };
        if (props.inProgress && !waiting && !speaking) {
                const shouldSpeak = !speaking && props.inProgress && props.workout && props.currentActivity && props.currentActivity.type === TimedActivityType.Round;
                if (shouldSpeak) {
                    const combination = combinationSelector.GetNextCombination();
                    const toSpeak = combination.strikes.join(" ");
                    setCurrentCombination(toSpeak);
                    //Start speaking text to speech
                    _speak(toSpeak);
                    //Start waiting
                    startWait();
                }
        }

    }, [speaking, waiting, props.inProgress]);

   

    const getWaitTimeInMilliseconds = () => {
        const waitTime = getPauseInMilliseconds();
        const waitFactor = currentCombination.length / 6;
        return waitTime * waitFactor;
    }

    const getPauseInMilliseconds = () => {
        switch (intensity) {
            case WorkoutIntensity.Low:
                return 5000;
            case WorkoutIntensity.High:
                return 3000;
            default:
                return 4000;
        }
    }

    return (<Text>{currentCombination}</Text>);

}

Hi

I have not seen callback functions defined within useEffect before. I’m not sure why that shouldn’t work, but have you tried defining them elsewhere?

Hi Wodin Thanks for your reply.
I tried to define the callback outside the useEffect block.
Tried to define then inline and tried simple callbacks like console.log but they still not get invoked.

One thing i am wondering is may be the issue is i am using the function component all the examples online are using class components. I will try the same scenarion with a class component and see if the outcome is any different.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.