在react-speech-kit中改变声音

发布于 2025-01-17 11:52:56 字数 569 浏览 0 评论 0原文

我正在使用React-Speech-kit的usEspeechsynesis来读取文本时,当单击按钮时。这是我的代码:

import React, { useState } from 'react';
import { useSpeechSynthesis } from 'react-speech-kit';
 
function Example() {
  const [value, setValue] = useState('');
  const { speak } = useSpeechSynthesis();
 
  return (
    <div>
      <textarea
        value={value}
        onChange={(event) => setValue(event.target.value)}
      />
      <button onClick={() => speak({ text: value })}>Speak</button>
    </div>
  );
}

默认语音是一个人。我如何改变声音

I am using useSpeechSynthesis of react-speech-kit to read the text when click button. This is my code:

import React, { useState } from 'react';
import { useSpeechSynthesis } from 'react-speech-kit';
 
function Example() {
  const [value, setValue] = useState('');
  const { speak } = useSpeechSynthesis();
 
  return (
    <div>
      <textarea
        value={value}
        onChange={(event) => setValue(event.target.value)}
      />
      <button onClick={() => speak({ text: value })}>Speak</button>
    </div>
  );
}

The default voice is a man. How can I change the voice

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

比忠 2025-01-24 11:52:56

我遇到了想要改变声音的同样问题。以下方法对我有用。

const {speen,voices} = usespeechsynthesis();

&lt; button onclick = {()=&gt;说话({text:value,语音:声音[1]})}&gt; speak&lt;/button&gt;

我从这个 link

I've encountered the same problem of wanting to change the voice. The following method worked for me.

const { speak, voices } = useSpeechSynthesis();

<button onClick={() => speak({ text: value, voice: voices[1] })}>Speak</button>

I've found the idea from this link.

忘年祭陌 2025-01-24 11:52:56

在 onClick 事件期间调用的 talk 函数中,还有一个可以在文本之外传递的键值,它是“语音”

import React, { useState } from 'react';
import { useSpeechSynthesis } from 'react-speech-kit';
 
function Example() {
  const [value, setValue] = useState('');
  const { speak } = useSpeechSynthesis();
 
  return (
    <div>
      <textarea
        value={value}
        onChange={(event) => setValue(event.target.value)}
      />
      <button onClick={() => speak({ text: value, voice:SpeechSynthesisVoice })}>Speak</button>
    </div>
  );
}

语音合成语音可在 voices 数组中使用。

希望这有帮助

In the speak function that you are calling during the onClick event there is an other key value that you can pass beyond text and it's "voice"

import React, { useState } from 'react';
import { useSpeechSynthesis } from 'react-speech-kit';
 
function Example() {
  const [value, setValue] = useState('');
  const { speak } = useSpeechSynthesis();
 
  return (
    <div>
      <textarea
        value={value}
        onChange={(event) => setValue(event.target.value)}
      />
      <button onClick={() => speak({ text: value, voice:SpeechSynthesisVoice })}>Speak</button>
    </div>
  );
}

The speech synthesis voice is available in the voices array.

Hope this helps

一影成城 2025-01-24 11:52:56

当我想将默认语音更改为女性意大利口音时,我遇到了同样的问题。以下方法对我有用。

import React, { useState } from 'react';
import { useSpeechSynthesis } from 'react-speech-kit';
        
function Example() {
    const { speak, voices } = useSpeechSynthesis();
    
    useEffect(() => {
        //console.log("You will get all voices here",voices);
        const selectedVoice = voices.find(voice => voice.lang.includes('it-IT'));
        speak({ text: "Put your text here", voice: selectedVoice });
    }, [])
}

I've encountered the same problem when I want to change voice from default, to a female Italian accent. The following method worked for me.

import React, { useState } from 'react';
import { useSpeechSynthesis } from 'react-speech-kit';
        
function Example() {
    const { speak, voices } = useSpeechSynthesis();
    
    useEffect(() => {
        //console.log("You will get all voices here",voices);
        const selectedVoice = voices.find(voice => voice.lang.includes('it-IT'));
        speak({ text: "Put your text here", voice: selectedVoice });
    }, [])
}
墟烟 2025-01-24 11:52:56

您可以在演示中找到每个语音名称:https://mikeyparton.github. io/react-speech-kit/

你可以通过调用speechSynthesis.getVoices()来获取可用语音的列表。确保在初始化语音合成并加载语音后调用此方法,例如在 useEffect 挂钩中。

这是我使用语音名称的函数:

const speakAllWords = async () => {
    const utterance = new SpeechSynthesisUtterance();
    utterance.voice = speechSynthesis
      .getVoices()
      .find((voice) => voice.name === "Microsoft David - English (United States)");
    for (let i = 0; i < words.length; i++) {
      setHighlightedWordIndex(i);
      utterance.text = words[i];
      speechSynthesis.speak(utterance);
      await wait(words[i].length * 250); // Adjust the multiplier for better synchronization
    }
    setHighlightedWordIndex(null);
  };

我的完整代码供参考:

import React, { useState, useEffect } from "react";
import { useSpeechSynthesis } from "react-speech-kit";

const TextHighlighter = ({ text }) => {
  const { speak, cancel, speaking } = useSpeechSynthesis();
  const [highlightedWordIndex, setHighlightedWordIndex] = useState(null);
  const [words, setWords] = useState([]);

  useEffect(() => {
    // Split text into words when text changes
    setWords(text.split(" "));
  }, [text]);

  const cancelSpeech = () => {
    cancel();
    setHighlightedWordIndex(null);
  };

  const speakAllWords = async () => {
    const utterance = new SpeechSynthesisUtterance();
    utterance.voice = speechSynthesis
      .getVoices()
      .find((voice) => voice.name === "Microsoft David - English (United States)");
    for (let i = 0; i < words.length; i++) {
      setHighlightedWordIndex(i);
      utterance.text = words[i];
      speechSynthesis.speak(utterance);
      await wait(words[i].length * 250); // Adjust the multiplier for better synchronization
    }
    setHighlightedWordIndex(null);
  };

  const wait = (ms) => {
    return new Promise((resolve) => setTimeout(resolve, ms));
  };

  return (
    <div>
      <p>
        {words.map((word, index) => (
          <span
            key={index}
            style={{
              backgroundColor:
                index === highlightedWordIndex ? "yellow" : "transparent",
            }}
          >
            {word}{" "}
          </span>
        ))}
      </p>
      <button onClick={speakAllWords}>Speak All</button>
      <button onClick={cancelSpeech}>Stop</button>
    </div>
  );
};

const App = () => {
  return (
    <div>
      <h1>Text-to-Speech with Highlighting</h1>
      <TextHighlighter text="The plumber fitted the pipes yesterday." />
    </div>
  );
};

export default App;

You can find every voice name in the demo: https://mikeyparton.github.io/react-speech-kit/

You can get a list of available voices by calling speechSynthesis.getVoices(). Make sure to call this method after the speech synthesis is initialized and voices are loaded, such as within a useEffect hook.

here is my function where voice name is used:

const speakAllWords = async () => {
    const utterance = new SpeechSynthesisUtterance();
    utterance.voice = speechSynthesis
      .getVoices()
      .find((voice) => voice.name === "Microsoft David - English (United States)");
    for (let i = 0; i < words.length; i++) {
      setHighlightedWordIndex(i);
      utterance.text = words[i];
      speechSynthesis.speak(utterance);
      await wait(words[i].length * 250); // Adjust the multiplier for better synchronization
    }
    setHighlightedWordIndex(null);
  };

My full code for the reference:

import React, { useState, useEffect } from "react";
import { useSpeechSynthesis } from "react-speech-kit";

const TextHighlighter = ({ text }) => {
  const { speak, cancel, speaking } = useSpeechSynthesis();
  const [highlightedWordIndex, setHighlightedWordIndex] = useState(null);
  const [words, setWords] = useState([]);

  useEffect(() => {
    // Split text into words when text changes
    setWords(text.split(" "));
  }, [text]);

  const cancelSpeech = () => {
    cancel();
    setHighlightedWordIndex(null);
  };

  const speakAllWords = async () => {
    const utterance = new SpeechSynthesisUtterance();
    utterance.voice = speechSynthesis
      .getVoices()
      .find((voice) => voice.name === "Microsoft David - English (United States)");
    for (let i = 0; i < words.length; i++) {
      setHighlightedWordIndex(i);
      utterance.text = words[i];
      speechSynthesis.speak(utterance);
      await wait(words[i].length * 250); // Adjust the multiplier for better synchronization
    }
    setHighlightedWordIndex(null);
  };

  const wait = (ms) => {
    return new Promise((resolve) => setTimeout(resolve, ms));
  };

  return (
    <div>
      <p>
        {words.map((word, index) => (
          <span
            key={index}
            style={{
              backgroundColor:
                index === highlightedWordIndex ? "yellow" : "transparent",
            }}
          >
            {word}{" "}
          </span>
        ))}
      </p>
      <button onClick={speakAllWords}>Speak All</button>
      <button onClick={cancelSpeech}>Stop</button>
    </div>
  );
};

const App = () => {
  return (
    <div>
      <h1>Text-to-Speech with Highlighting</h1>
      <TextHighlighter text="The plumber fitted the pipes yesterday." />
    </div>
  );
};

export default App;

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文