首页 > 解决方案 > 在 IOS 上反应语音到文本和文本到语音

问题描述

我对反应真的很陌生,我正在尝试使用反应创建一个语音到文本应用程序,我使用这个模块https://github.com/MikeyParton/react-speech-kit。这是我的语音识别功能

function SpeechRecognition() {
  const [lang, setLang] = useState("en-US");
  const [value, setValue] = useState("");
  const [blocked, setBlocked] = useState(false);

  const onEnd = () => {
    // You could do something here after listening has finished
  };

  const onResult = (result) => {
    setValue(result);
  };

  const changeLang = (event) => {
    setLang(event.target.value);
  };

  const onError = (event) => {
    if (event.error === "not-allowed") {
      setBlocked(true);
    }
  };

  const { listen, listening, stop, supported } = useSpeechRecognition({
    onResult,
    onEnd,
    onError,
  });

  const toggle = listening
    ? stop
    : () => {
        setBlocked(false);
        listen({ lang });
      };

  return (
      <button disabled={blocked} type="button" onClick={toggle}>
          {listening ? (
            <FontAwesomeIcon icon={faMicrophoneSlash} />
          ) : (
            <FontAwesomeIcon icon={faMicrophone} />
          )}
        </button>
  );
}

它可以在任何浏览器上的 android 和 windows 上运行,但无论是 safari 还是 chrome,我都无法在 IOS 上运行。在我研究了 IOS webspeech API 之后,我发现了这篇文章http://iwearshorts.com/blog/ios-safari-partial-support-of-web-speech-api/但我不知道如何使用这行代码在我的代码上

var SpeechRecognition = SpeechRecognition || webkitSpeechRecognition || null;

https://bory-webspeech.netlify.app/我当前开发的演示

编辑

我改变这条线

const { listen, listening, stop, supported } = useSpeechRecognition({

const SpeechRecognition = useSpeechRecognition|| webkitSpeechRecognition || null;
`const { listen, listening, stop, supported } = SpeechRecognition ({`

但在IOS上它仍然无法正常工作

标签: javascriptreactjs

解决方案


推荐阅读