根据博览会演讲中的文字将文字转为语音

sma*_*mal 5 javascript react-native expo

我想知道这可能是一个简单的问题:我应该使用什么方法让 expo-speak 中的 Speech.speak 在每个分隔符(如空格)处调用函数或回调。需要明确的是,这是为了反应本机

例如,如果我要求它说“嘿,这是一个有趣的句子”,我希望它在每个单词之后运行一个函数。

我目前认为它与 onMark 或 onBoundary 方法有关,这些方法是用源代码编写的,几乎没有文档

这是源代码中的语音选项:

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;
};
Run Code Online (Sandbox Code Playgroud)

这就是我想要运行的

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);
      }
    });
Run Code Online (Sandbox Code Playgroud)

我已经尝试过 onMark 和 onBoundary 的明显组合,但无法让它工作。非常感谢,如果应该使用另一个文本到语音库,只要它使用 expo go,我很乐意这样做。

Ang*_*eno 0

我试图自己做这件事。我的目标是突出显示当前正在朗读的文本。这是我迄今为止在 IOS 模拟器上运行的内容:

const inputValue = 'Hey this is an interesting sentence';
Speech.speak(inputValue, {
    onBoundary:(boundaries) => {
        const {charIndex, charLength} = boundaries;
        const word = inputValue.substring(charIndex, charIndex+charLength)
        console.log(boundaries, word)
    }
});
Run Code Online (Sandbox Code Playgroud)

说话时日志输出:

LOG  {"charIndex": 0, "charLength": 3} Hey
LOG  {"charIndex": 4, "charLength": 4} this
LOG  {"charIndex": 9, "charLength": 2} is
LOG  {"charIndex": 12, "charLength": 2} an
LOG  {"charIndex": 15, "charLength": 11} interesting
LOG  {"charIndex": 27, "charLength": 8} sentence
Run Code Online (Sandbox Code Playgroud)

如您所见,仅显示文字。如果您想要中间有空格(又名暂停),只需假设它是charIndex+charLength+1

我做了一份小吃,但该小吃仅适用于网络:https ://snack.expo.dev/@angelxmoreno/speechtest?platform=web