Follow words said in text to speech from expo-speech

Viewed 52

I was wondering what is probably a simple question: What method should I use to have Speech.speak from expo-speak call a function or callback at every delimiter like a space. To be clear this is for react Native

For example if I ask it to say "Hey this is an interesting sentence", I would like it to run a function after each word.

I currently believe that it has something to do with the methods onMark or onBoundary which are written in source code with little to no documentation

This is the Speech options in source code:

export type SpeechOptions = {
  /**
   * The code of a language that should be used to read the `text`, refer to IETF BCP 47 to see
   * valid codes.
   */
  language?: string;
  /**
   * Pitch of the voice to speak `text`. `1.0` is the normal pitch.
   */
  pitch?: number;
  /**
   * Rate of the voice to speak `text`. `1.0` is the normal rate.
   */
  rate?: number;
  /**
   * A callback that is invoked when speaking starts.
   */
  onStart?: () => void | SpeechEventCallback;
  /**
   * A callback that is invoked when speaking is stopped by calling `Speech.stop()`.
   */
  onStopped?: () => void | SpeechEventCallback;
  /**
   * A callback that is invoked when speaking finishes.
   */
  onDone?: () => void | SpeechEventCallback;
  /**
   * A callback that is invoked when an error occurred while speaking.
   * @param error
   * @platform android
   */
  onError?: (error: Error) => void | SpeechEventCallback;
  /**
   * Volume of the voice to speak `text`. A number between `0.0` (muted) and `1.0` (max volume)
   *
   * @default 1.0
   * @platform web
   */
  volume?: number;
  /**
   * Voice identifier.
   */
  voice?: string;
  _voiceIndex?: number;
  onBoundary?: SpeechEventCallback | null; // This is what I am wondering about
  onMark?: SpeechEventCallback | null;
  onPause?: SpeechEventCallback | null;
  onResume?: SpeechEventCallback | null;
};

and here is what I am trying to run

Speech.speak(someText,{
      language: 'en-US',
      pitch: 1,
      rate: 1,
      onMark: (event) => { // I want this to run every time a space happens
        console.log(typeof event);
      }
    });

I have tried obvious combinations of onMark and onBoundary but cant get it to work. Thank you so much and if another text to speech library should be used I would be happy to do it as long as it uses expo go.

0 Answers
Related