当前位置:首页 > 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
分享给朋友:

相关文章

react中monent如何获取日期

react中monent如何获取日期

使用 Moment.js 获取当前日期 在 React 中通过 Moment.js 获取当前日期,可以直接调用 moment() 函数。它会返回包含当前日期和时间的 Moment 对象。 impor…

react如何开发组件

react如何开发组件

React 组件开发基础 React 组件分为函数组件和类组件两种形式。函数组件是现代 React 开发的主流方式,结合 Hooks 可以实现完整功能。 函数组件示例: function Gree…

react如何调度

react如何调度

React 调度机制概述 React 的调度机制通过 Fiber 架构 和 Scheduler 模块实现任务优先级管理与时间切片(Time Slicing),确保高优先级更新(如用户交互)能快速响应,…

react如何分

react如何分

React 分页实现方法 在 React 中实现分页功能可以通过多种方式完成,以下是几种常见的方法: 使用状态管理分页数据 在组件内部维护当前页码和每页数据量的状态,通过计算切片数据实现分页。…

react如何减少伤病

react如何减少伤病

减少React应用中的性能问题(避免"伤病") 优化React应用性能需要从组件渲染、状态管理和外部依赖等多方面入手。以下是关键优化策略: 使用React.memo进行组件记忆 对函数组件使用Rea…

如何实现语音react

如何实现语音react

语音识别基础设置 在React中实现语音识别功能通常需要借助浏览器的Web Speech API或第三方库。Web Speech API提供了SpeechRecognition接口,允许应用程序直接访…