当前位置:首页 > React

react实现语音播报

2026-01-27 03:41:58React

使用 Web Speech API 实现语音播报

React 中可以通过浏览器原生支持的 Web Speech API 实现语音播报功能。该 API 提供了 SpeechSynthesis 接口,允许将文本转换为语音。

安装依赖(可选):

npm install react-speech-kit

基本实现代码:

react实现语音播报

import React, { useState } from 'react';

const SpeechComponent = () => {
  const [text, setText] = useState('');

  const speak = () => {
    const utterance = new SpeechSynthesisUtterance(text);
    window.speechSynthesis.speak(utterance);
  };

  return (
    <div>
      <input 
        type="text" 
        value={text} 
        onChange={(e) => setText(e.target.value)} 
      />
      <button onClick={speak}>播放语音</button>
    </div>
  );
};

export default SpeechComponent;

自定义语音参数

可以调整语音的音调、速率和音量等参数:

const speakWithOptions = () => {
  const utterance = new SpeechSynthesisUtterance(text);
  utterance.rate = 1.2;  // 语速 (0.1-10)
  utterance.pitch = 1;   // 音高 (0-2)
  utterance.volume = 1;  // 音量 (0-1)

  // 获取可用语音列表并选择特定语音
  const voices = window.speechSynthesis.getVoices();
  utterance.voice = voices.find(voice => voice.name.includes('Microsoft'));

  window.speechSynthesis.speak(utterance);
};

使用第三方库 react-speech-kit

如需更简单的实现,可以使用 react-speech-kit 库:

react实现语音播报

import { useSpeechSynthesis } from 'react-speech-kit';

const SpeechComponent = () => {
  const [text, setText] = useState('');
  const { speak } = useSpeechSynthesis();

  return (
    <div>
      <input 
        type="text" 
        value={text} 
        onChange={(e) => setText(e.target.value)} 
      />
      <button onClick={() => speak({ text })}>播放语音</button>
    </div>
  );
};

注意事项

Web Speech API 的兼容性需要考虑:

  • 主流现代浏览器均支持(Chrome、Edge、Firefox、Safari)
  • iOS 上需要用户交互才能触发语音
  • 首次使用时可能需要获取用户权限

语音合成功能在组件卸载时应取消:

useEffect(() => {
  return () => {
    window.speechSynthesis.cancel();
  };
}, []);

高级功能实现

实现语音队列和中断功能:

const [queue, setQueue] = useState([]);

const addToQueue = (newText) => {
  setQueue(prev => [...prev, newText]);
};

useEffect(() => {
  if (queue.length > 0 && !window.speechSynthesis.speaking) {
    const utterance = new SpeechSynthesisUtterance(queue[0]);
    utterance.onend = () => {
      setQueue(prev => prev.slice(1));
    };
    window.speechSynthesis.speak(utterance);
  }
}, [queue]);

标签: 语音react
分享给朋友:

相关文章

vue实现语音

vue实现语音

Vue 实现语音功能 在 Vue 中实现语音功能通常涉及语音识别(Speech-to-Text)和语音合成(Text-to-Speech)。以下是具体实现方法: 语音识别(Speech-to-Tex…

如何评价react native

如何评价react native

React Native 的优势 跨平台开发效率高:基于 JavaScript 和 React 语法,可同时开发 iOS 和 Android 应用,减少重复代码量。性能接近原生:通过原生组件渲染,性能…

react如何取消渲染

react如何取消渲染

取消渲染的方法 在React中,取消渲染通常指的是在某些条件下阻止组件渲染或中断正在进行的渲染过程。以下是几种常见的方法: 条件渲染 通过条件判断决定是否渲染组件或部分内容。可以使用if语句或三元运…

react如何读

react如何读

React 的发音 React 的发音为 /riˈækt/(音标),读作“瑞-艾克特”。其中: “Re” 发音类似英文单词 “read” 的开头部分。 “act” 发音与英文单词 “act” 一致。…

react如何验证

react如何验证

表单验证方法 在React中验证表单数据通常使用以下几种方式: 内置HTML5验证 利用HTML5原生表单验证属性如required、pattern等,结合form.noValidate属性禁用浏…

react如何encode

react如何encode

React 中编码的实现方式 在 React 中,编码通常涉及对 URL、HTML 实体或特殊字符的处理。以下是几种常见的编码方法: 使用 encodeURIComponent 进行 URL 编码…